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. 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. default:
  151. panic("arch_setup_msi_irq: Invalid octeon_dma_bar_type");
  152. }
  153. msg.data = irq - OCTEON_IRQ_MSI_BIT0;
  154. /* Update the number of IRQs the device has available to it */
  155. control &= ~PCI_MSI_FLAGS_QSIZE;
  156. control |= request_private_bits << 4;
  157. pci_write_config_word(dev, desc->msi_attrib.pos + PCI_MSI_FLAGS,
  158. control);
  159. irq_set_msi_desc(irq, desc);
  160. write_msi_msg(irq, &msg);
  161. return 0;
  162. }
  163. int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  164. {
  165. struct msi_desc *entry;
  166. int ret;
  167. /*
  168. * MSI-X is not supported.
  169. */
  170. if (type == PCI_CAP_ID_MSIX)
  171. return -EINVAL;
  172. /*
  173. * If an architecture wants to support multiple MSI, it needs to
  174. * override arch_setup_msi_irqs()
  175. */
  176. if (type == PCI_CAP_ID_MSI && nvec > 1)
  177. return 1;
  178. list_for_each_entry(entry, &dev->msi_list, list) {
  179. ret = arch_setup_msi_irq(dev, entry);
  180. if (ret < 0)
  181. return ret;
  182. if (ret > 0)
  183. return -ENOSPC;
  184. }
  185. return 0;
  186. }
  187. /**
  188. * Called when a device no longer needs its MSI interrupts. All
  189. * MSI interrupts for the device are freed.
  190. *
  191. * @irq: The devices first irq number. There may be multple in sequence.
  192. */
  193. void arch_teardown_msi_irq(unsigned int irq)
  194. {
  195. int number_irqs;
  196. u64 bitmask;
  197. int index = 0;
  198. int irq0;
  199. if ((irq < OCTEON_IRQ_MSI_BIT0)
  200. || (irq > msi_irq_size + OCTEON_IRQ_MSI_BIT0))
  201. panic("arch_teardown_msi_irq: Attempted to teardown illegal "
  202. "MSI interrupt (%d)", irq);
  203. irq -= OCTEON_IRQ_MSI_BIT0;
  204. index = irq / 64;
  205. irq0 = irq % 64;
  206. /*
  207. * Count the number of IRQs we need to free by looking at the
  208. * msi_multiple_irq_bitmask. Each bit set means that the next
  209. * IRQ is also owned by this device.
  210. */
  211. number_irqs = 0;
  212. while ((irq0 + number_irqs < 64) &&
  213. (msi_multiple_irq_bitmask[index]
  214. & (1ull << (irq0 + number_irqs))))
  215. number_irqs++;
  216. number_irqs++;
  217. /* Mask with one bit for each IRQ */
  218. bitmask = (1 << number_irqs) - 1;
  219. /* Shift the mask to the correct bit location */
  220. bitmask <<= irq0;
  221. if ((msi_free_irq_bitmask[index] & bitmask) != bitmask)
  222. panic("arch_teardown_msi_irq: Attempted to teardown MSI "
  223. "interrupt (%d) not in use", irq);
  224. /* Checks are done, update the in use bitmask */
  225. spin_lock(&msi_free_irq_bitmask_lock);
  226. msi_free_irq_bitmask[index] &= ~bitmask;
  227. msi_multiple_irq_bitmask[index] &= ~bitmask;
  228. spin_unlock(&msi_free_irq_bitmask_lock);
  229. }
  230. static DEFINE_RAW_SPINLOCK(octeon_irq_msi_lock);
  231. static u64 msi_rcv_reg[4];
  232. static u64 mis_ena_reg[4];
  233. static void octeon_irq_msi_enable_pcie(struct irq_data *data)
  234. {
  235. u64 en;
  236. unsigned long flags;
  237. int msi_number = data->irq - OCTEON_IRQ_MSI_BIT0;
  238. int irq_index = msi_number >> 6;
  239. int irq_bit = msi_number & 0x3f;
  240. raw_spin_lock_irqsave(&octeon_irq_msi_lock, flags);
  241. en = cvmx_read_csr(mis_ena_reg[irq_index]);
  242. en |= 1ull << irq_bit;
  243. cvmx_write_csr(mis_ena_reg[irq_index], en);
  244. cvmx_read_csr(mis_ena_reg[irq_index]);
  245. raw_spin_unlock_irqrestore(&octeon_irq_msi_lock, flags);
  246. }
  247. static void octeon_irq_msi_disable_pcie(struct irq_data *data)
  248. {
  249. u64 en;
  250. unsigned long flags;
  251. int msi_number = data->irq - OCTEON_IRQ_MSI_BIT0;
  252. int irq_index = msi_number >> 6;
  253. int irq_bit = msi_number & 0x3f;
  254. raw_spin_lock_irqsave(&octeon_irq_msi_lock, flags);
  255. en = cvmx_read_csr(mis_ena_reg[irq_index]);
  256. en &= ~(1ull << irq_bit);
  257. cvmx_write_csr(mis_ena_reg[irq_index], en);
  258. cvmx_read_csr(mis_ena_reg[irq_index]);
  259. raw_spin_unlock_irqrestore(&octeon_irq_msi_lock, flags);
  260. }
  261. static struct irq_chip octeon_irq_chip_msi_pcie = {
  262. .name = "MSI",
  263. .irq_enable = octeon_irq_msi_enable_pcie,
  264. .irq_disable = octeon_irq_msi_disable_pcie,
  265. };
  266. static void octeon_irq_msi_enable_pci(struct irq_data *data)
  267. {
  268. /*
  269. * Octeon PCI doesn't have the ability to mask/unmask MSI
  270. * interrupts individually. Instead of masking/unmasking them
  271. * in groups of 16, we simple assume MSI devices are well
  272. * behaved. MSI interrupts are always enable and the ACK is
  273. * assumed to be enough
  274. */
  275. }
  276. static void octeon_irq_msi_disable_pci(struct irq_data *data)
  277. {
  278. /* See comment in enable */
  279. }
  280. static struct irq_chip octeon_irq_chip_msi_pci = {
  281. .name = "MSI",
  282. .irq_enable = octeon_irq_msi_enable_pci,
  283. .irq_disable = octeon_irq_msi_disable_pci,
  284. };
  285. /*
  286. * Called by the interrupt handling code when an MSI interrupt
  287. * occurs.
  288. */
  289. static irqreturn_t __octeon_msi_do_interrupt(int index, u64 msi_bits)
  290. {
  291. int irq;
  292. int bit;
  293. bit = fls64(msi_bits);
  294. if (bit) {
  295. bit--;
  296. /* Acknowledge it first. */
  297. cvmx_write_csr(msi_rcv_reg[index], 1ull << bit);
  298. irq = bit + OCTEON_IRQ_MSI_BIT0 + 64 * index;
  299. do_IRQ(irq);
  300. return IRQ_HANDLED;
  301. }
  302. return IRQ_NONE;
  303. }
  304. #define OCTEON_MSI_INT_HANDLER_X(x) \
  305. static irqreturn_t octeon_msi_interrupt##x(int cpl, void *dev_id) \
  306. { \
  307. u64 msi_bits = cvmx_read_csr(msi_rcv_reg[(x)]); \
  308. return __octeon_msi_do_interrupt((x), msi_bits); \
  309. }
  310. /*
  311. * Create octeon_msi_interrupt{0-3} function body
  312. */
  313. OCTEON_MSI_INT_HANDLER_X(0);
  314. OCTEON_MSI_INT_HANDLER_X(1);
  315. OCTEON_MSI_INT_HANDLER_X(2);
  316. OCTEON_MSI_INT_HANDLER_X(3);
  317. /*
  318. * Initializes the MSI interrupt handling code
  319. */
  320. int __init octeon_msi_initialize(void)
  321. {
  322. int irq;
  323. struct irq_chip *msi;
  324. if (octeon_dma_bar_type == OCTEON_DMA_BAR_TYPE_PCIE) {
  325. msi_rcv_reg[0] = CVMX_PEXP_NPEI_MSI_RCV0;
  326. msi_rcv_reg[1] = CVMX_PEXP_NPEI_MSI_RCV1;
  327. msi_rcv_reg[2] = CVMX_PEXP_NPEI_MSI_RCV2;
  328. msi_rcv_reg[3] = CVMX_PEXP_NPEI_MSI_RCV3;
  329. mis_ena_reg[0] = CVMX_PEXP_NPEI_MSI_ENB0;
  330. mis_ena_reg[1] = CVMX_PEXP_NPEI_MSI_ENB1;
  331. mis_ena_reg[2] = CVMX_PEXP_NPEI_MSI_ENB2;
  332. mis_ena_reg[3] = CVMX_PEXP_NPEI_MSI_ENB3;
  333. msi = &octeon_irq_chip_msi_pcie;
  334. } else {
  335. msi_rcv_reg[0] = CVMX_NPI_NPI_MSI_RCV;
  336. #define INVALID_GENERATE_ADE 0x8700000000000000ULL;
  337. msi_rcv_reg[1] = INVALID_GENERATE_ADE;
  338. msi_rcv_reg[2] = INVALID_GENERATE_ADE;
  339. msi_rcv_reg[3] = INVALID_GENERATE_ADE;
  340. mis_ena_reg[0] = INVALID_GENERATE_ADE;
  341. mis_ena_reg[1] = INVALID_GENERATE_ADE;
  342. mis_ena_reg[2] = INVALID_GENERATE_ADE;
  343. mis_ena_reg[3] = INVALID_GENERATE_ADE;
  344. msi = &octeon_irq_chip_msi_pci;
  345. }
  346. for (irq = OCTEON_IRQ_MSI_BIT0; irq <= OCTEON_IRQ_MSI_LAST; irq++)
  347. irq_set_chip_and_handler(irq, msi, handle_simple_irq);
  348. if (octeon_has_feature(OCTEON_FEATURE_PCIE)) {
  349. if (request_irq(OCTEON_IRQ_PCI_MSI0, octeon_msi_interrupt0,
  350. 0, "MSI[0:63]", octeon_msi_interrupt0))
  351. panic("request_irq(OCTEON_IRQ_PCI_MSI0) failed");
  352. if (request_irq(OCTEON_IRQ_PCI_MSI1, octeon_msi_interrupt1,
  353. 0, "MSI[64:127]", octeon_msi_interrupt1))
  354. panic("request_irq(OCTEON_IRQ_PCI_MSI1) failed");
  355. if (request_irq(OCTEON_IRQ_PCI_MSI2, octeon_msi_interrupt2,
  356. 0, "MSI[127:191]", octeon_msi_interrupt2))
  357. panic("request_irq(OCTEON_IRQ_PCI_MSI2) failed");
  358. if (request_irq(OCTEON_IRQ_PCI_MSI3, octeon_msi_interrupt3,
  359. 0, "MSI[192:255]", octeon_msi_interrupt3))
  360. panic("request_irq(OCTEON_IRQ_PCI_MSI3) failed");
  361. msi_irq_size = 256;
  362. } else if (octeon_is_pci_host()) {
  363. if (request_irq(OCTEON_IRQ_PCI_MSI0, octeon_msi_interrupt0,
  364. 0, "MSI[0:15]", octeon_msi_interrupt0))
  365. panic("request_irq(OCTEON_IRQ_PCI_MSI0) failed");
  366. if (request_irq(OCTEON_IRQ_PCI_MSI1, octeon_msi_interrupt0,
  367. 0, "MSI[16:31]", octeon_msi_interrupt0))
  368. panic("request_irq(OCTEON_IRQ_PCI_MSI1) failed");
  369. if (request_irq(OCTEON_IRQ_PCI_MSI2, octeon_msi_interrupt0,
  370. 0, "MSI[32:47]", octeon_msi_interrupt0))
  371. panic("request_irq(OCTEON_IRQ_PCI_MSI2) failed");
  372. if (request_irq(OCTEON_IRQ_PCI_MSI3, octeon_msi_interrupt0,
  373. 0, "MSI[48:63]", octeon_msi_interrupt0))
  374. panic("request_irq(OCTEON_IRQ_PCI_MSI3) failed");
  375. msi_irq_size = 64;
  376. }
  377. return 0;
  378. }
  379. subsys_initcall(octeon_msi_initialize);