msi-octeon.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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-pexp-defs.h>
  18. #include <asm/octeon/pci-octeon.h>
  19. /*
  20. * Each bit in msi_free_irq_bitmask represents a MSI interrupt that is
  21. * in use.
  22. */
  23. static u64 msi_free_irq_bitmask[4];
  24. /*
  25. * Each bit in msi_multiple_irq_bitmask tells that the device using
  26. * this bit in msi_free_irq_bitmask is also using the next bit. This
  27. * is used so we can disable all of the MSI interrupts when a device
  28. * uses multiple.
  29. */
  30. static u64 msi_multiple_irq_bitmask[4];
  31. /*
  32. * This lock controls updates to msi_free_irq_bitmask and
  33. * msi_multiple_irq_bitmask.
  34. */
  35. static DEFINE_SPINLOCK(msi_free_irq_bitmask_lock);
  36. /*
  37. * Number of MSI IRQs used. This variable is set up in
  38. * the module init time.
  39. */
  40. static int msi_irq_size;
  41. /**
  42. * Called when a driver request MSI interrupts instead of the
  43. * legacy INT A-D. This routine will allocate multiple interrupts
  44. * for MSI devices that support them. A device can override this by
  45. * programming the MSI control bits [6:4] before calling
  46. * pci_enable_msi().
  47. *
  48. * @dev: Device requesting MSI interrupts
  49. * @desc: MSI descriptor
  50. *
  51. * Returns 0 on success.
  52. */
  53. int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
  54. {
  55. struct msi_msg msg;
  56. u16 control;
  57. int configured_private_bits;
  58. int request_private_bits;
  59. int irq = 0;
  60. int irq_step;
  61. u64 search_mask;
  62. int index;
  63. /*
  64. * Read the MSI config to figure out how many IRQs this device
  65. * wants. Most devices only want 1, which will give
  66. * configured_private_bits and request_private_bits equal 0.
  67. */
  68. pci_read_config_word(dev, desc->msi_attrib.pos + PCI_MSI_FLAGS,
  69. &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. case OCTEON_DMA_BAR_TYPE_BIG:
  139. /* When using big bar, Bar 0 is based at 0 */
  140. msg.address_lo = (0 + CVMX_PCI_MSI_RCV) & 0xffffffff;
  141. msg.address_hi = (0 + CVMX_PCI_MSI_RCV) >> 32;
  142. break;
  143. case OCTEON_DMA_BAR_TYPE_PCIE:
  144. /* When using PCIe, Bar 0 is based at 0 */
  145. /* FIXME CVMX_NPEI_MSI_RCV* other than 0? */
  146. msg.address_lo = (0 + CVMX_NPEI_PCIE_MSI_RCV) & 0xffffffff;
  147. msg.address_hi = (0 + CVMX_NPEI_PCIE_MSI_RCV) >> 32;
  148. break;
  149. default:
  150. panic("arch_setup_msi_irq: Invalid octeon_dma_bar_type");
  151. }
  152. msg.data = irq - OCTEON_IRQ_MSI_BIT0;
  153. /* Update the number of IRQs the device has available to it */
  154. control &= ~PCI_MSI_FLAGS_QSIZE;
  155. control |= request_private_bits << 4;
  156. pci_write_config_word(dev, desc->msi_attrib.pos + PCI_MSI_FLAGS,
  157. control);
  158. irq_set_msi_desc(irq, desc);
  159. write_msi_msg(irq, &msg);
  160. return 0;
  161. }
  162. int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  163. {
  164. struct msi_desc *entry;
  165. int ret;
  166. /*
  167. * MSI-X is not supported.
  168. */
  169. if (type == PCI_CAP_ID_MSIX)
  170. return -EINVAL;
  171. /*
  172. * If an architecture wants to support multiple MSI, it needs to
  173. * override arch_setup_msi_irqs()
  174. */
  175. if (type == PCI_CAP_ID_MSI && nvec > 1)
  176. return 1;
  177. list_for_each_entry(entry, &dev->msi_list, list) {
  178. ret = arch_setup_msi_irq(dev, entry);
  179. if (ret < 0)
  180. return ret;
  181. if (ret > 0)
  182. return -ENOSPC;
  183. }
  184. return 0;
  185. }
  186. /**
  187. * Called when a device no longer needs its MSI interrupts. All
  188. * MSI interrupts for the device are freed.
  189. *
  190. * @irq: The devices first irq number. There may be multple in sequence.
  191. */
  192. void arch_teardown_msi_irq(unsigned int irq)
  193. {
  194. int number_irqs;
  195. u64 bitmask;
  196. int index = 0;
  197. int irq0;
  198. if ((irq < OCTEON_IRQ_MSI_BIT0)
  199. || (irq > msi_irq_size + OCTEON_IRQ_MSI_BIT0))
  200. panic("arch_teardown_msi_irq: Attempted to teardown illegal "
  201. "MSI interrupt (%d)", irq);
  202. irq -= OCTEON_IRQ_MSI_BIT0;
  203. index = irq / 64;
  204. irq0 = irq % 64;
  205. /*
  206. * Count the number of IRQs we need to free by looking at the
  207. * msi_multiple_irq_bitmask. Each bit set means that the next
  208. * IRQ is also owned by this device.
  209. */
  210. number_irqs = 0;
  211. while ((irq0 + number_irqs < 64) &&
  212. (msi_multiple_irq_bitmask[index]
  213. & (1ull << (irq0 + number_irqs))))
  214. number_irqs++;
  215. number_irqs++;
  216. /* Mask with one bit for each IRQ */
  217. bitmask = (1 << number_irqs) - 1;
  218. /* Shift the mask to the correct bit location */
  219. bitmask <<= irq0;
  220. if ((msi_free_irq_bitmask[index] & bitmask) != bitmask)
  221. panic("arch_teardown_msi_irq: Attempted to teardown MSI "
  222. "interrupt (%d) not in use", irq);
  223. /* Checks are done, update the in use bitmask */
  224. spin_lock(&msi_free_irq_bitmask_lock);
  225. msi_free_irq_bitmask[index] &= ~bitmask;
  226. msi_multiple_irq_bitmask[index] &= ~bitmask;
  227. spin_unlock(&msi_free_irq_bitmask_lock);
  228. }
  229. static DEFINE_RAW_SPINLOCK(octeon_irq_msi_lock);
  230. static u64 msi_rcv_reg[4];
  231. static u64 mis_ena_reg[4];
  232. static void octeon_irq_msi_enable_pcie(struct irq_data *data)
  233. {
  234. u64 en;
  235. unsigned long flags;
  236. int msi_number = data->irq - OCTEON_IRQ_MSI_BIT0;
  237. int irq_index = msi_number >> 6;
  238. int irq_bit = msi_number & 0x3f;
  239. raw_spin_lock_irqsave(&octeon_irq_msi_lock, flags);
  240. en = cvmx_read_csr(mis_ena_reg[irq_index]);
  241. en |= 1ull << irq_bit;
  242. cvmx_write_csr(mis_ena_reg[irq_index], en);
  243. cvmx_read_csr(mis_ena_reg[irq_index]);
  244. raw_spin_unlock_irqrestore(&octeon_irq_msi_lock, flags);
  245. }
  246. static void octeon_irq_msi_disable_pcie(struct irq_data *data)
  247. {
  248. u64 en;
  249. unsigned long flags;
  250. int msi_number = data->irq - OCTEON_IRQ_MSI_BIT0;
  251. int irq_index = msi_number >> 6;
  252. int irq_bit = msi_number & 0x3f;
  253. raw_spin_lock_irqsave(&octeon_irq_msi_lock, flags);
  254. en = cvmx_read_csr(mis_ena_reg[irq_index]);
  255. en &= ~(1ull << irq_bit);
  256. cvmx_write_csr(mis_ena_reg[irq_index], en);
  257. cvmx_read_csr(mis_ena_reg[irq_index]);
  258. raw_spin_unlock_irqrestore(&octeon_irq_msi_lock, flags);
  259. }
  260. static struct irq_chip octeon_irq_chip_msi_pcie = {
  261. .name = "MSI",
  262. .irq_enable = octeon_irq_msi_enable_pcie,
  263. .irq_disable = octeon_irq_msi_disable_pcie,
  264. };
  265. static void octeon_irq_msi_enable_pci(struct irq_data *data)
  266. {
  267. /*
  268. * Octeon PCI doesn't have the ability to mask/unmask MSI
  269. * interrupts individually. Instead of masking/unmasking them
  270. * in groups of 16, we simple assume MSI devices are well
  271. * behaved. MSI interrupts are always enable and the ACK is
  272. * assumed to be enough
  273. */
  274. }
  275. static void octeon_irq_msi_disable_pci(struct irq_data *data)
  276. {
  277. /* See comment in enable */
  278. }
  279. static struct irq_chip octeon_irq_chip_msi_pci = {
  280. .name = "MSI",
  281. .irq_enable = octeon_irq_msi_enable_pci,
  282. .irq_disable = octeon_irq_msi_disable_pci,
  283. };
  284. /*
  285. * Called by the interrupt handling code when an MSI interrupt
  286. * occurs.
  287. */
  288. static irqreturn_t __octeon_msi_do_interrupt(int index, u64 msi_bits)
  289. {
  290. int irq;
  291. int bit;
  292. bit = fls64(msi_bits);
  293. if (bit) {
  294. bit--;
  295. /* Acknowledge it first. */
  296. cvmx_write_csr(msi_rcv_reg[index], 1ull << bit);
  297. irq = bit + OCTEON_IRQ_MSI_BIT0 + 64 * index;
  298. do_IRQ(irq);
  299. return IRQ_HANDLED;
  300. }
  301. return IRQ_NONE;
  302. }
  303. #define OCTEON_MSI_INT_HANDLER_X(x) \
  304. static irqreturn_t octeon_msi_interrupt##x(int cpl, void *dev_id) \
  305. { \
  306. u64 msi_bits = cvmx_read_csr(msi_rcv_reg[(x)]); \
  307. return __octeon_msi_do_interrupt((x), msi_bits); \
  308. }
  309. /*
  310. * Create octeon_msi_interrupt{0-3} function body
  311. */
  312. OCTEON_MSI_INT_HANDLER_X(0);
  313. OCTEON_MSI_INT_HANDLER_X(1);
  314. OCTEON_MSI_INT_HANDLER_X(2);
  315. OCTEON_MSI_INT_HANDLER_X(3);
  316. /*
  317. * Initializes the MSI interrupt handling code
  318. */
  319. int __init octeon_msi_initialize(void)
  320. {
  321. int irq;
  322. struct irq_chip *msi;
  323. if (octeon_dma_bar_type == OCTEON_DMA_BAR_TYPE_PCIE) {
  324. msi_rcv_reg[0] = CVMX_PEXP_NPEI_MSI_RCV0;
  325. msi_rcv_reg[1] = CVMX_PEXP_NPEI_MSI_RCV1;
  326. msi_rcv_reg[2] = CVMX_PEXP_NPEI_MSI_RCV2;
  327. msi_rcv_reg[3] = CVMX_PEXP_NPEI_MSI_RCV3;
  328. mis_ena_reg[0] = CVMX_PEXP_NPEI_MSI_ENB0;
  329. mis_ena_reg[1] = CVMX_PEXP_NPEI_MSI_ENB1;
  330. mis_ena_reg[2] = CVMX_PEXP_NPEI_MSI_ENB2;
  331. mis_ena_reg[3] = CVMX_PEXP_NPEI_MSI_ENB3;
  332. msi = &octeon_irq_chip_msi_pcie;
  333. } else {
  334. msi_rcv_reg[0] = CVMX_NPI_NPI_MSI_RCV;
  335. #define INVALID_GENERATE_ADE 0x8700000000000000ULL;
  336. msi_rcv_reg[1] = INVALID_GENERATE_ADE;
  337. msi_rcv_reg[2] = INVALID_GENERATE_ADE;
  338. msi_rcv_reg[3] = INVALID_GENERATE_ADE;
  339. mis_ena_reg[0] = INVALID_GENERATE_ADE;
  340. mis_ena_reg[1] = INVALID_GENERATE_ADE;
  341. mis_ena_reg[2] = INVALID_GENERATE_ADE;
  342. mis_ena_reg[3] = INVALID_GENERATE_ADE;
  343. msi = &octeon_irq_chip_msi_pci;
  344. }
  345. for (irq = OCTEON_IRQ_MSI_BIT0; irq <= OCTEON_IRQ_MSI_LAST; irq++)
  346. irq_set_chip_and_handler(irq, msi, handle_simple_irq);
  347. if (octeon_has_feature(OCTEON_FEATURE_PCIE)) {
  348. if (request_irq(OCTEON_IRQ_PCI_MSI0, octeon_msi_interrupt0,
  349. 0, "MSI[0:63]", octeon_msi_interrupt0))
  350. panic("request_irq(OCTEON_IRQ_PCI_MSI0) failed");
  351. if (request_irq(OCTEON_IRQ_PCI_MSI1, octeon_msi_interrupt1,
  352. 0, "MSI[64:127]", octeon_msi_interrupt1))
  353. panic("request_irq(OCTEON_IRQ_PCI_MSI1) failed");
  354. if (request_irq(OCTEON_IRQ_PCI_MSI2, octeon_msi_interrupt2,
  355. 0, "MSI[127:191]", octeon_msi_interrupt2))
  356. panic("request_irq(OCTEON_IRQ_PCI_MSI2) failed");
  357. if (request_irq(OCTEON_IRQ_PCI_MSI3, octeon_msi_interrupt3,
  358. 0, "MSI[192:255]", octeon_msi_interrupt3))
  359. panic("request_irq(OCTEON_IRQ_PCI_MSI3) failed");
  360. msi_irq_size = 256;
  361. } else if (octeon_is_pci_host()) {
  362. if (request_irq(OCTEON_IRQ_PCI_MSI0, octeon_msi_interrupt0,
  363. 0, "MSI[0:15]", octeon_msi_interrupt0))
  364. panic("request_irq(OCTEON_IRQ_PCI_MSI0) failed");
  365. if (request_irq(OCTEON_IRQ_PCI_MSI1, octeon_msi_interrupt0,
  366. 0, "MSI[16:31]", octeon_msi_interrupt0))
  367. panic("request_irq(OCTEON_IRQ_PCI_MSI1) failed");
  368. if (request_irq(OCTEON_IRQ_PCI_MSI2, octeon_msi_interrupt0,
  369. 0, "MSI[32:47]", octeon_msi_interrupt0))
  370. panic("request_irq(OCTEON_IRQ_PCI_MSI2) failed");
  371. if (request_irq(OCTEON_IRQ_PCI_MSI3, octeon_msi_interrupt0,
  372. 0, "MSI[48:63]", octeon_msi_interrupt0))
  373. panic("request_irq(OCTEON_IRQ_PCI_MSI3) failed");
  374. msi_irq_size = 64;
  375. }
  376. return 0;
  377. }
  378. subsys_initcall(octeon_msi_initialize);