123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- /*
- * Intel MIC Platform Software Stack (MPSS)
- *
- * Copyright(c) 2013 Intel Corporation.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License, version 2, as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * The full GNU General Public License is included in this distribution in
- * the file called "COPYING".
- *
- * Disclaimer: The codes contained in these modules may be specific to
- * the Intel Software Development Platform codenamed: Knights Ferry, and
- * the Intel product codenamed: Knights Corner, and are not backward
- * compatible with other Intel products. Additionally, Intel will NOT
- * support the codes or instruction set in future products.
- *
- * Intel MIC Card driver.
- *
- */
- #include <linux/module.h>
- #include <linux/pci.h>
- #include <linux/interrupt.h>
- #include <linux/reboot.h>
- #include <linux/dmaengine.h>
- #include <linux/kmod.h>
- #include <linux/mic_common.h>
- #include "../common/mic_dev.h"
- #include "mic_device.h"
- static struct mic_driver *g_drv;
- static int __init mic_dp_init(void)
- {
- struct mic_driver *mdrv = g_drv;
- struct mic_device *mdev = &mdrv->mdev;
- struct mic_bootparam __iomem *bootparam;
- u64 lo, hi, dp_dma_addr;
- u32 magic;
- lo = mic_read_spad(&mdrv->mdev, MIC_DPLO_SPAD);
- hi = mic_read_spad(&mdrv->mdev, MIC_DPHI_SPAD);
- dp_dma_addr = lo | (hi << 32);
- mdrv->dp = mic_card_map(mdev, dp_dma_addr, MIC_DP_SIZE);
- if (!mdrv->dp) {
- dev_err(mdrv->dev, "Cannot remap Aperture BAR\n");
- return -ENOMEM;
- }
- bootparam = mdrv->dp;
- magic = ioread32(&bootparam->magic);
- if (MIC_MAGIC != magic) {
- dev_err(mdrv->dev, "bootparam magic mismatch 0x%x\n", magic);
- return -EIO;
- }
- return 0;
- }
- /* Uninitialize the device page */
- static void mic_dp_uninit(void)
- {
- mic_card_unmap(&g_drv->mdev, g_drv->dp);
- }
- /**
- * mic_request_card_irq - request an irq.
- *
- * @handler: interrupt handler passed to request_threaded_irq.
- * @thread_fn: thread fn. passed to request_threaded_irq.
- * @name: The ASCII name of the callee requesting the irq.
- * @data: private data that is returned back when calling the
- * function handler.
- * @index: The doorbell index of the requester.
- *
- * returns: The cookie that is transparent to the caller. Passed
- * back when calling mic_free_irq. An appropriate error code
- * is returned on failure. Caller needs to use IS_ERR(return_val)
- * to check for failure and PTR_ERR(return_val) to obtained the
- * error code.
- *
- */
- struct mic_irq *
- mic_request_card_irq(irq_handler_t handler,
- irq_handler_t thread_fn, const char *name,
- void *data, int index)
- {
- int rc = 0;
- unsigned long cookie;
- struct mic_driver *mdrv = g_drv;
- rc = request_threaded_irq(mic_db_to_irq(mdrv, index), handler,
- thread_fn, 0, name, data);
- if (rc) {
- dev_err(mdrv->dev, "request_threaded_irq failed rc = %d\n", rc);
- goto err;
- }
- mdrv->irq_info.irq_usage_count[index]++;
- cookie = index;
- return (struct mic_irq *)cookie;
- err:
- return ERR_PTR(rc);
- }
- /**
- * mic_free_card_irq - free irq.
- *
- * @cookie: cookie obtained during a successful call to mic_request_threaded_irq
- * @data: private data specified by the calling function during the
- * mic_request_threaded_irq
- *
- * returns: none.
- */
- void mic_free_card_irq(struct mic_irq *cookie, void *data)
- {
- int index;
- struct mic_driver *mdrv = g_drv;
- index = (unsigned long)cookie & 0xFFFFU;
- free_irq(mic_db_to_irq(mdrv, index), data);
- mdrv->irq_info.irq_usage_count[index]--;
- }
- /**
- * mic_next_card_db - Get the doorbell with minimum usage count.
- *
- * Returns the irq index.
- */
- int mic_next_card_db(void)
- {
- int i;
- int index = 0;
- struct mic_driver *mdrv = g_drv;
- for (i = 0; i < mdrv->intr_info.num_intr; i++) {
- if (mdrv->irq_info.irq_usage_count[i] <
- mdrv->irq_info.irq_usage_count[index])
- index = i;
- }
- return index;
- }
- /**
- * mic_init_irq - Initialize irq information.
- *
- * Returns 0 in success. Appropriate error code on failure.
- */
- static int mic_init_irq(void)
- {
- struct mic_driver *mdrv = g_drv;
- mdrv->irq_info.irq_usage_count = kzalloc((sizeof(u32) *
- mdrv->intr_info.num_intr),
- GFP_KERNEL);
- if (!mdrv->irq_info.irq_usage_count)
- return -ENOMEM;
- return 0;
- }
- /**
- * mic_uninit_irq - Uninitialize irq information.
- *
- * None.
- */
- static void mic_uninit_irq(void)
- {
- struct mic_driver *mdrv = g_drv;
- kfree(mdrv->irq_info.irq_usage_count);
- }
- static inline struct mic_driver *scdev_to_mdrv(struct scif_hw_dev *scdev)
- {
- return dev_get_drvdata(scdev->dev.parent);
- }
- static struct mic_irq *
- ___mic_request_irq(struct scif_hw_dev *scdev,
- irqreturn_t (*func)(int irq, void *data),
- const char *name, void *data,
- int db)
- {
- return mic_request_card_irq(func, NULL, name, data, db);
- }
- static void
- ___mic_free_irq(struct scif_hw_dev *scdev,
- struct mic_irq *cookie, void *data)
- {
- return mic_free_card_irq(cookie, data);
- }
- static void ___mic_ack_interrupt(struct scif_hw_dev *scdev, int num)
- {
- struct mic_driver *mdrv = scdev_to_mdrv(scdev);
- mic_ack_interrupt(&mdrv->mdev);
- }
- static int ___mic_next_db(struct scif_hw_dev *scdev)
- {
- return mic_next_card_db();
- }
- static void ___mic_send_intr(struct scif_hw_dev *scdev, int db)
- {
- struct mic_driver *mdrv = scdev_to_mdrv(scdev);
- mic_send_intr(&mdrv->mdev, db);
- }
- static void ___mic_send_p2p_intr(struct scif_hw_dev *scdev, int db,
- struct mic_mw *mw)
- {
- mic_send_p2p_intr(db, mw);
- }
- static void __iomem *
- ___mic_ioremap(struct scif_hw_dev *scdev,
- phys_addr_t pa, size_t len)
- {
- struct mic_driver *mdrv = scdev_to_mdrv(scdev);
- return mic_card_map(&mdrv->mdev, pa, len);
- }
- static void ___mic_iounmap(struct scif_hw_dev *scdev, void __iomem *va)
- {
- struct mic_driver *mdrv = scdev_to_mdrv(scdev);
- mic_card_unmap(&mdrv->mdev, va);
- }
- static struct scif_hw_ops scif_hw_ops = {
- .request_irq = ___mic_request_irq,
- .free_irq = ___mic_free_irq,
- .ack_interrupt = ___mic_ack_interrupt,
- .next_db = ___mic_next_db,
- .send_intr = ___mic_send_intr,
- .send_p2p_intr = ___mic_send_p2p_intr,
- .ioremap = ___mic_ioremap,
- .iounmap = ___mic_iounmap,
- };
- static inline struct mic_driver *vpdev_to_mdrv(struct vop_device *vpdev)
- {
- return dev_get_drvdata(vpdev->dev.parent);
- }
- static struct mic_irq *
- __mic_request_irq(struct vop_device *vpdev,
- irqreturn_t (*func)(int irq, void *data),
- const char *name, void *data, int intr_src)
- {
- return mic_request_card_irq(func, NULL, name, data, intr_src);
- }
- static void __mic_free_irq(struct vop_device *vpdev,
- struct mic_irq *cookie, void *data)
- {
- return mic_free_card_irq(cookie, data);
- }
- static void __mic_ack_interrupt(struct vop_device *vpdev, int num)
- {
- struct mic_driver *mdrv = vpdev_to_mdrv(vpdev);
- mic_ack_interrupt(&mdrv->mdev);
- }
- static int __mic_next_db(struct vop_device *vpdev)
- {
- return mic_next_card_db();
- }
- static void __iomem *__mic_get_remote_dp(struct vop_device *vpdev)
- {
- struct mic_driver *mdrv = vpdev_to_mdrv(vpdev);
- return mdrv->dp;
- }
- static void __mic_send_intr(struct vop_device *vpdev, int db)
- {
- struct mic_driver *mdrv = vpdev_to_mdrv(vpdev);
- mic_send_intr(&mdrv->mdev, db);
- }
- static void __iomem *__mic_ioremap(struct vop_device *vpdev,
- dma_addr_t pa, size_t len)
- {
- struct mic_driver *mdrv = vpdev_to_mdrv(vpdev);
- return mic_card_map(&mdrv->mdev, pa, len);
- }
- static void __mic_iounmap(struct vop_device *vpdev, void __iomem *va)
- {
- struct mic_driver *mdrv = vpdev_to_mdrv(vpdev);
- mic_card_unmap(&mdrv->mdev, va);
- }
- static struct vop_hw_ops vop_hw_ops = {
- .request_irq = __mic_request_irq,
- .free_irq = __mic_free_irq,
- .ack_interrupt = __mic_ack_interrupt,
- .next_db = __mic_next_db,
- .get_remote_dp = __mic_get_remote_dp,
- .send_intr = __mic_send_intr,
- .ioremap = __mic_ioremap,
- .iounmap = __mic_iounmap,
- };
- static int mic_request_dma_chans(struct mic_driver *mdrv)
- {
- dma_cap_mask_t mask;
- struct dma_chan *chan;
- dma_cap_zero(mask);
- dma_cap_set(DMA_MEMCPY, mask);
- do {
- chan = dma_request_channel(mask, NULL, NULL);
- if (chan) {
- mdrv->dma_ch[mdrv->num_dma_ch++] = chan;
- if (mdrv->num_dma_ch >= MIC_MAX_DMA_CHAN)
- break;
- }
- } while (chan);
- dev_info(mdrv->dev, "DMA channels # %d\n", mdrv->num_dma_ch);
- return mdrv->num_dma_ch;
- }
- static void mic_free_dma_chans(struct mic_driver *mdrv)
- {
- int i = 0;
- for (i = 0; i < mdrv->num_dma_ch; i++) {
- dma_release_channel(mdrv->dma_ch[i]);
- mdrv->dma_ch[i] = NULL;
- }
- mdrv->num_dma_ch = 0;
- }
- /*
- * mic_driver_init - MIC driver initialization tasks.
- *
- * Returns 0 in success. Appropriate error code on failure.
- */
- int __init mic_driver_init(struct mic_driver *mdrv)
- {
- int rc;
- struct mic_bootparam __iomem *bootparam;
- u8 node_id;
- g_drv = mdrv;
- /* Unloading the card module is not supported. */
- if (!try_module_get(mdrv->dev->driver->owner)) {
- rc = -ENODEV;
- goto done;
- }
- rc = mic_dp_init();
- if (rc)
- goto put;
- rc = mic_init_irq();
- if (rc)
- goto dp_uninit;
- if (!mic_request_dma_chans(mdrv)) {
- rc = -ENODEV;
- goto irq_uninit;
- }
- mdrv->vpdev = vop_register_device(mdrv->dev, VOP_DEV_TRNSP,
- NULL, &vop_hw_ops, 0,
- NULL, mdrv->dma_ch[0]);
- if (IS_ERR(mdrv->vpdev)) {
- rc = PTR_ERR(mdrv->vpdev);
- goto dma_free;
- }
- bootparam = mdrv->dp;
- node_id = ioread8(&bootparam->node_id);
- mdrv->scdev = scif_register_device(mdrv->dev, MIC_SCIF_DEV,
- NULL, &scif_hw_ops,
- 0, node_id, &mdrv->mdev.mmio, NULL,
- NULL, mdrv->dp, mdrv->dma_ch,
- mdrv->num_dma_ch, true);
- if (IS_ERR(mdrv->scdev)) {
- rc = PTR_ERR(mdrv->scdev);
- goto vop_remove;
- }
- mic_create_card_debug_dir(mdrv);
- done:
- return rc;
- vop_remove:
- vop_unregister_device(mdrv->vpdev);
- dma_free:
- mic_free_dma_chans(mdrv);
- irq_uninit:
- mic_uninit_irq();
- dp_uninit:
- mic_dp_uninit();
- put:
- module_put(mdrv->dev->driver->owner);
- return rc;
- }
- /*
- * mic_driver_uninit - MIC driver uninitialization tasks.
- *
- * Returns None
- */
- void mic_driver_uninit(struct mic_driver *mdrv)
- {
- mic_delete_card_debug_dir(mdrv);
- scif_unregister_device(mdrv->scdev);
- vop_unregister_device(mdrv->vpdev);
- mic_free_dma_chans(mdrv);
- mic_uninit_irq();
- mic_dp_uninit();
- module_put(mdrv->dev->driver->owner);
- }
|