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
6 * Copyright (c) 2005 Silicon Graphics, Inc. All rights reserved.
10 * MOATB Core Services driver.
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/types.h>
17 #include <linux/ioport.h>
18 #include <linux/kernel.h>
19 #include <linux/notifier.h>
20 #include <linux/reboot.h>
21 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/device.h>
26 #include <linux/uio.h>
27 #include <linux/mutex.h>
28 #include <linux/slab.h>
30 #include <asm/uaccess.h>
31 #include <asm/system.h>
32 #include <asm/pgtable.h>
33 #include <asm/sn/addrs.h>
34 #include <asm/sn/intr.h>
35 #include <asm/sn/tiocx.h>
40 #define DBG(fmt...) printk(KERN_ALERT fmt)
44 static DEFINE_MUTEX(mbcs_mutex);
45 static int mbcs_major;
47 static LIST_HEAD(soft_list);
52 static const struct file_operations mbcs_ops = {
54 .llseek = mbcs_sram_llseek,
55 .read = mbcs_sram_read,
56 .write = mbcs_sram_write,
57 .mmap = mbcs_gscr_mmap,
60 struct mbcs_callback_arg {
62 struct cx_dev *cx_dev;
65 static inline void mbcs_getdma_init(struct getdma *gdma)
67 memset(gdma, 0, sizeof(struct getdma));
68 gdma->DoneIntEnable = 1;
71 static inline void mbcs_putdma_init(struct putdma *pdma)
73 memset(pdma, 0, sizeof(struct putdma));
74 pdma->DoneIntEnable = 1;
77 static inline void mbcs_algo_init(struct algoblock *algo_soft)
79 memset(algo_soft, 0, sizeof(struct algoblock));
82 static inline void mbcs_getdma_set(void *mmr,
91 uint64_t amoModType, uint64_t intrHostDest,
94 union dma_control rdma_control;
95 union dma_amo_dest amo_dest;
96 union intr_dest intr_dest;
97 union dma_localaddr local_addr;
98 union dma_hostaddr host_addr;
100 rdma_control.dma_control_reg = 0;
101 amo_dest.dma_amo_dest_reg = 0;
102 intr_dest.intr_dest_reg = 0;
103 local_addr.dma_localaddr_reg = 0;
104 host_addr.dma_hostaddr_reg = 0;
106 host_addr.dma_sys_addr = hostAddr;
107 MBCS_MMR_SET(mmr, MBCS_RD_DMA_SYS_ADDR, host_addr.dma_hostaddr_reg);
109 local_addr.dma_ram_addr = localAddr;
110 local_addr.dma_ram_sel = localRamSel;
111 MBCS_MMR_SET(mmr, MBCS_RD_DMA_LOC_ADDR, local_addr.dma_localaddr_reg);
113 rdma_control.dma_op_length = numPkts;
114 rdma_control.done_amo_en = amoEnable;
115 rdma_control.done_int_en = intrEnable;
116 rdma_control.pio_mem_n = peerIO;
117 MBCS_MMR_SET(mmr, MBCS_RD_DMA_CTRL, rdma_control.dma_control_reg);
119 amo_dest.dma_amo_sys_addr = amoHostDest;
120 amo_dest.dma_amo_mod_type = amoModType;
121 MBCS_MMR_SET(mmr, MBCS_RD_DMA_AMO_DEST, amo_dest.dma_amo_dest_reg);
123 intr_dest.address = intrHostDest;
124 intr_dest.int_vector = intrVector;
125 MBCS_MMR_SET(mmr, MBCS_RD_DMA_INT_DEST, intr_dest.intr_dest_reg);
129 static inline void mbcs_putdma_set(void *mmr,
132 uint64_t localRamSel,
137 uint64_t amoHostDest,
139 uint64_t intrHostDest, uint64_t intrVector)
141 union dma_control wdma_control;
142 union dma_amo_dest amo_dest;
143 union intr_dest intr_dest;
144 union dma_localaddr local_addr;
145 union dma_hostaddr host_addr;
147 wdma_control.dma_control_reg = 0;
148 amo_dest.dma_amo_dest_reg = 0;
149 intr_dest.intr_dest_reg = 0;
150 local_addr.dma_localaddr_reg = 0;
151 host_addr.dma_hostaddr_reg = 0;
153 host_addr.dma_sys_addr = hostAddr;
154 MBCS_MMR_SET(mmr, MBCS_WR_DMA_SYS_ADDR, host_addr.dma_hostaddr_reg);
156 local_addr.dma_ram_addr = localAddr;
157 local_addr.dma_ram_sel = localRamSel;
158 MBCS_MMR_SET(mmr, MBCS_WR_DMA_LOC_ADDR, local_addr.dma_localaddr_reg);
160 wdma_control.dma_op_length = numPkts;
161 wdma_control.done_amo_en = amoEnable;
162 wdma_control.done_int_en = intrEnable;
163 wdma_control.pio_mem_n = peerIO;
164 MBCS_MMR_SET(mmr, MBCS_WR_DMA_CTRL, wdma_control.dma_control_reg);
166 amo_dest.dma_amo_sys_addr = amoHostDest;
167 amo_dest.dma_amo_mod_type = amoModType;
168 MBCS_MMR_SET(mmr, MBCS_WR_DMA_AMO_DEST, amo_dest.dma_amo_dest_reg);
170 intr_dest.address = intrHostDest;
171 intr_dest.int_vector = intrVector;
172 MBCS_MMR_SET(mmr, MBCS_WR_DMA_INT_DEST, intr_dest.intr_dest_reg);
176 static inline void mbcs_algo_set(void *mmr,
177 uint64_t amoHostDest,
179 uint64_t intrHostDest,
180 uint64_t intrVector, uint64_t algoStepCount)
182 union dma_amo_dest amo_dest;
183 union intr_dest intr_dest;
184 union algo_step step;
186 step.algo_step_reg = 0;
187 intr_dest.intr_dest_reg = 0;
188 amo_dest.dma_amo_dest_reg = 0;
190 amo_dest.dma_amo_sys_addr = amoHostDest;
191 amo_dest.dma_amo_mod_type = amoModType;
192 MBCS_MMR_SET(mmr, MBCS_ALG_AMO_DEST, amo_dest.dma_amo_dest_reg);
194 intr_dest.address = intrHostDest;
195 intr_dest.int_vector = intrVector;
196 MBCS_MMR_SET(mmr, MBCS_ALG_INT_DEST, intr_dest.intr_dest_reg);
198 step.alg_step_cnt = algoStepCount;
199 MBCS_MMR_SET(mmr, MBCS_ALG_STEP, step.algo_step_reg);
202 static inline int mbcs_getdma_start(struct mbcs_soft *soft)
207 union cm_control cm_control;
209 mmr_base = soft->mmr_base;
210 gdma = &soft->getdma;
212 /* check that host address got setup */
217 (gdma->bytes + (MBCS_CACHELINE_SIZE - 1)) / MBCS_CACHELINE_SIZE;
220 mbcs_getdma_set(mmr_base, tiocx_dma_addr(gdma->hostAddr),
222 (gdma->localAddr < MB2) ? 0 :
223 (gdma->localAddr < MB4) ? 1 :
224 (gdma->localAddr < MB6) ? 2 : 3,
231 gdma->intrHostDest, gdma->intrVector);
234 cm_control.cm_control_reg = MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
235 cm_control.rd_dma_go = 1;
236 MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL, cm_control.cm_control_reg);
242 static inline int mbcs_putdma_start(struct mbcs_soft *soft)
247 union cm_control cm_control;
249 mmr_base = soft->mmr_base;
250 pdma = &soft->putdma;
252 /* check that host address got setup */
257 (pdma->bytes + (MBCS_CACHELINE_SIZE - 1)) / MBCS_CACHELINE_SIZE;
260 mbcs_putdma_set(mmr_base, tiocx_dma_addr(pdma->hostAddr),
262 (pdma->localAddr < MB2) ? 0 :
263 (pdma->localAddr < MB4) ? 1 :
264 (pdma->localAddr < MB6) ? 2 : 3,
271 pdma->intrHostDest, pdma->intrVector);
274 cm_control.cm_control_reg = MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
275 cm_control.wr_dma_go = 1;
276 MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL, cm_control.cm_control_reg);
282 static inline int mbcs_algo_start(struct mbcs_soft *soft)
284 struct algoblock *algo_soft = &soft->algo;
285 void *mmr_base = soft->mmr_base;
286 union cm_control cm_control;
288 if (mutex_lock_interruptible(&soft->algolock))
291 atomic_set(&soft->algo_done, 0);
293 mbcs_algo_set(mmr_base,
294 algo_soft->amoHostDest,
295 algo_soft->amoModType,
296 algo_soft->intrHostDest,
297 algo_soft->intrVector, algo_soft->algoStepCount);
299 /* start algorithm */
300 cm_control.cm_control_reg = MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
301 cm_control.alg_done_int_en = 1;
302 cm_control.alg_go = 1;
303 MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL, cm_control.cm_control_reg);
305 mutex_unlock(&soft->algolock);
310 static inline ssize_t
311 do_mbcs_sram_dmawrite(struct mbcs_soft *soft, uint64_t hostAddr,
312 size_t len, loff_t * off)
316 if (mutex_lock_interruptible(&soft->dmawritelock))
319 atomic_set(&soft->dmawrite_done, 0);
321 soft->putdma.hostAddr = hostAddr;
322 soft->putdma.localAddr = *off;
323 soft->putdma.bytes = len;
325 if (mbcs_putdma_start(soft) < 0) {
326 DBG(KERN_ALERT "do_mbcs_sram_dmawrite: "
327 "mbcs_putdma_start failed\n");
332 if (wait_event_interruptible(soft->dmawrite_queue,
333 atomic_read(&soft->dmawrite_done))) {
342 mutex_unlock(&soft->dmawritelock);
347 static inline ssize_t
348 do_mbcs_sram_dmaread(struct mbcs_soft *soft, uint64_t hostAddr,
349 size_t len, loff_t * off)
353 if (mutex_lock_interruptible(&soft->dmareadlock))
356 atomic_set(&soft->dmawrite_done, 0);
358 soft->getdma.hostAddr = hostAddr;
359 soft->getdma.localAddr = *off;
360 soft->getdma.bytes = len;
362 if (mbcs_getdma_start(soft) < 0) {
363 DBG(KERN_ALERT "mbcs_strategy: mbcs_getdma_start failed\n");
368 if (wait_event_interruptible(soft->dmaread_queue,
369 atomic_read(&soft->dmaread_done))) {
378 mutex_unlock(&soft->dmareadlock);
383 static int mbcs_open(struct inode *ip, struct file *fp)
385 struct mbcs_soft *soft;
388 mutex_lock(&mbcs_mutex);
391 /* Nothing protects access to this list... */
392 list_for_each_entry(soft, &soft_list, list) {
393 if (soft->nasid == minor) {
394 fp->private_data = soft->cxdev;
395 mutex_unlock(&mbcs_mutex);
400 mutex_unlock(&mbcs_mutex);
404 static ssize_t mbcs_sram_read(struct file * fp, char __user *buf, size_t len, loff_t * off)
406 struct cx_dev *cx_dev = fp->private_data;
407 struct mbcs_soft *soft = cx_dev->soft;
411 hostAddr = __get_dma_pages(GFP_KERNEL, get_order(len));
415 rv = do_mbcs_sram_dmawrite(soft, hostAddr, len, off);
419 if (copy_to_user(buf, (void *)hostAddr, len))
423 free_pages(hostAddr, get_order(len));
429 mbcs_sram_write(struct file * fp, const char __user *buf, size_t len, loff_t * off)
431 struct cx_dev *cx_dev = fp->private_data;
432 struct mbcs_soft *soft = cx_dev->soft;
436 hostAddr = __get_dma_pages(GFP_KERNEL, get_order(len));
440 if (copy_from_user((void *)hostAddr, buf, len)) {
445 rv = do_mbcs_sram_dmaread(soft, hostAddr, len, off);
448 free_pages(hostAddr, get_order(len));
453 static loff_t mbcs_sram_llseek(struct file * filp, loff_t off, int whence)
463 newpos = filp->f_pos + off;
467 newpos = MBCS_SRAM_SIZE + off;
470 default: /* can't happen */
477 filp->f_pos = newpos;
482 static uint64_t mbcs_pioaddr(struct mbcs_soft *soft, uint64_t offset)
486 mmr_base = (uint64_t) (soft->mmr_base + offset);
491 static void mbcs_debug_pioaddr_set(struct mbcs_soft *soft)
493 soft->debug_addr = mbcs_pioaddr(soft, MBCS_DEBUG_START);
496 static void mbcs_gscr_pioaddr_set(struct mbcs_soft *soft)
498 soft->gscr_addr = mbcs_pioaddr(soft, MBCS_GSCR_START);
501 static int mbcs_gscr_mmap(struct file *fp, struct vm_area_struct *vma)
503 struct cx_dev *cx_dev = fp->private_data;
504 struct mbcs_soft *soft = cx_dev->soft;
506 if (vma->vm_pgoff != 0)
509 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
511 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
512 if (remap_pfn_range(vma,
514 __pa(soft->gscr_addr) >> PAGE_SHIFT,
523 * mbcs_completion_intr_handler - Primary completion handler.
525 * @arg: soft struct for device
529 mbcs_completion_intr_handler(int irq, void *arg)
531 struct mbcs_soft *soft = (struct mbcs_soft *)arg;
533 union cm_status cm_status;
534 union cm_control cm_control;
536 mmr_base = soft->mmr_base;
537 cm_status.cm_status_reg = MBCS_MMR_GET(mmr_base, MBCS_CM_STATUS);
539 if (cm_status.rd_dma_done) {
540 /* stop dma-read engine, clear status */
541 cm_control.cm_control_reg =
542 MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
543 cm_control.rd_dma_clr = 1;
544 MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL,
545 cm_control.cm_control_reg);
546 atomic_set(&soft->dmaread_done, 1);
547 wake_up(&soft->dmaread_queue);
549 if (cm_status.wr_dma_done) {
550 /* stop dma-write engine, clear status */
551 cm_control.cm_control_reg =
552 MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
553 cm_control.wr_dma_clr = 1;
554 MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL,
555 cm_control.cm_control_reg);
556 atomic_set(&soft->dmawrite_done, 1);
557 wake_up(&soft->dmawrite_queue);
559 if (cm_status.alg_done) {
561 cm_control.cm_control_reg =
562 MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
563 cm_control.alg_done_clr = 1;
564 MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL,
565 cm_control.cm_control_reg);
566 atomic_set(&soft->algo_done, 1);
567 wake_up(&soft->algo_queue);
574 * mbcs_intr_alloc - Allocate interrupts.
575 * @dev: device pointer
578 static int mbcs_intr_alloc(struct cx_dev *dev)
580 struct sn_irq_info *sn_irq;
581 struct mbcs_soft *soft;
582 struct getdma *getdma;
583 struct putdma *putdma;
584 struct algoblock *algo;
587 getdma = &soft->getdma;
588 putdma = &soft->putdma;
591 soft->get_sn_irq = NULL;
592 soft->put_sn_irq = NULL;
593 soft->algo_sn_irq = NULL;
595 sn_irq = tiocx_irq_alloc(dev->cx_id.nasid, TIOCX_CORELET, -1, -1, -1);
598 soft->get_sn_irq = sn_irq;
599 getdma->intrHostDest = sn_irq->irq_xtalkaddr;
600 getdma->intrVector = sn_irq->irq_irq;
601 if (request_irq(sn_irq->irq_irq,
602 (void *)mbcs_completion_intr_handler, IRQF_SHARED,
603 "MBCS get intr", (void *)soft)) {
604 tiocx_irq_free(soft->get_sn_irq);
608 sn_irq = tiocx_irq_alloc(dev->cx_id.nasid, TIOCX_CORELET, -1, -1, -1);
609 if (sn_irq == NULL) {
610 free_irq(soft->get_sn_irq->irq_irq, soft);
611 tiocx_irq_free(soft->get_sn_irq);
614 soft->put_sn_irq = sn_irq;
615 putdma->intrHostDest = sn_irq->irq_xtalkaddr;
616 putdma->intrVector = sn_irq->irq_irq;
617 if (request_irq(sn_irq->irq_irq,
618 (void *)mbcs_completion_intr_handler, IRQF_SHARED,
619 "MBCS put intr", (void *)soft)) {
620 tiocx_irq_free(soft->put_sn_irq);
621 free_irq(soft->get_sn_irq->irq_irq, soft);
622 tiocx_irq_free(soft->get_sn_irq);
626 sn_irq = tiocx_irq_alloc(dev->cx_id.nasid, TIOCX_CORELET, -1, -1, -1);
627 if (sn_irq == NULL) {
628 free_irq(soft->put_sn_irq->irq_irq, soft);
629 tiocx_irq_free(soft->put_sn_irq);
630 free_irq(soft->get_sn_irq->irq_irq, soft);
631 tiocx_irq_free(soft->get_sn_irq);
634 soft->algo_sn_irq = sn_irq;
635 algo->intrHostDest = sn_irq->irq_xtalkaddr;
636 algo->intrVector = sn_irq->irq_irq;
637 if (request_irq(sn_irq->irq_irq,
638 (void *)mbcs_completion_intr_handler, IRQF_SHARED,
639 "MBCS algo intr", (void *)soft)) {
640 tiocx_irq_free(soft->algo_sn_irq);
641 free_irq(soft->put_sn_irq->irq_irq, soft);
642 tiocx_irq_free(soft->put_sn_irq);
643 free_irq(soft->get_sn_irq->irq_irq, soft);
644 tiocx_irq_free(soft->get_sn_irq);
652 * mbcs_intr_dealloc - Remove interrupts.
653 * @dev: device pointer
656 static void mbcs_intr_dealloc(struct cx_dev *dev)
658 struct mbcs_soft *soft;
662 free_irq(soft->get_sn_irq->irq_irq, soft);
663 tiocx_irq_free(soft->get_sn_irq);
664 free_irq(soft->put_sn_irq->irq_irq, soft);
665 tiocx_irq_free(soft->put_sn_irq);
666 free_irq(soft->algo_sn_irq->irq_irq, soft);
667 tiocx_irq_free(soft->algo_sn_irq);
670 static inline int mbcs_hw_init(struct mbcs_soft *soft)
672 void *mmr_base = soft->mmr_base;
673 union cm_control cm_control;
674 union cm_req_timeout cm_req_timeout;
677 cm_req_timeout.cm_req_timeout_reg =
678 MBCS_MMR_GET(mmr_base, MBCS_CM_REQ_TOUT);
680 cm_req_timeout.time_out = MBCS_CM_CONTROL_REQ_TOUT_MASK;
681 MBCS_MMR_SET(mmr_base, MBCS_CM_REQ_TOUT,
682 cm_req_timeout.cm_req_timeout_reg);
684 mbcs_gscr_pioaddr_set(soft);
685 mbcs_debug_pioaddr_set(soft);
688 err_stat = MBCS_MMR_GET(mmr_base, MBCS_CM_ERR_STAT);
689 MBCS_MMR_SET(mmr_base, MBCS_CM_CLR_ERR_STAT, err_stat);
690 MBCS_MMR_ZERO(mmr_base, MBCS_CM_ERROR_DETAIL1);
692 /* enable interrupts */
693 /* turn off 2^23 (INT_EN_PIO_REQ_ADDR_INV) */
694 MBCS_MMR_SET(mmr_base, MBCS_CM_ERR_INT_EN, 0x3ffffff7e00ffUL);
696 /* arm status regs and clear engines */
697 cm_control.cm_control_reg = MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
698 cm_control.rearm_stat_regs = 1;
699 cm_control.alg_clr = 1;
700 cm_control.wr_dma_clr = 1;
701 cm_control.rd_dma_clr = 1;
703 MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL, cm_control.cm_control_reg);
708 static ssize_t show_algo(struct device *dev, struct device_attribute *attr, char *buf)
710 struct cx_dev *cx_dev = to_cx_dev(dev);
711 struct mbcs_soft *soft = cx_dev->soft;
715 * By convention, the first debug register contains the
716 * algorithm number and revision.
718 debug0 = *(uint64_t *) soft->debug_addr;
720 return sprintf(buf, "0x%x 0x%x\n",
721 upper_32_bits(debug0), lower_32_bits(debug0));
724 static ssize_t store_algo(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
727 struct cx_dev *cx_dev = to_cx_dev(dev);
728 struct mbcs_soft *soft = cx_dev->soft;
733 n = simple_strtoul(buf, NULL, 0);
736 mbcs_algo_start(soft);
737 if (wait_event_interruptible(soft->algo_queue,
738 atomic_read(&soft->algo_done)))
745 DEVICE_ATTR(algo, 0644, show_algo, store_algo);
748 * mbcs_probe - Initialize for device
749 * @dev: device pointer
750 * @device_id: id table pointer
753 static int mbcs_probe(struct cx_dev *dev, const struct cx_device_id *id)
755 struct mbcs_soft *soft;
759 soft = kzalloc(sizeof(struct mbcs_soft), GFP_KERNEL);
763 soft->nasid = dev->cx_id.nasid;
764 list_add(&soft->list, &soft_list);
765 soft->mmr_base = (void *)tiocx_swin_base(dev->cx_id.nasid);
769 init_waitqueue_head(&soft->dmawrite_queue);
770 init_waitqueue_head(&soft->dmaread_queue);
771 init_waitqueue_head(&soft->algo_queue);
773 mutex_init(&soft->dmawritelock);
774 mutex_init(&soft->dmareadlock);
775 mutex_init(&soft->algolock);
777 mbcs_getdma_init(&soft->getdma);
778 mbcs_putdma_init(&soft->putdma);
779 mbcs_algo_init(&soft->algo);
783 /* Allocate interrupts */
784 mbcs_intr_alloc(dev);
786 device_create_file(&dev->dev, &dev_attr_algo);
791 static int mbcs_remove(struct cx_dev *dev)
794 mbcs_intr_dealloc(dev);
798 device_remove_file(&dev->dev, &dev_attr_algo);
803 static const struct cx_device_id __devinitdata mbcs_id_table[] = {
805 .part_num = MBCS_PART_NUM,
806 .mfg_num = MBCS_MFG_NUM,
809 .part_num = MBCS_PART_NUM_ALG0,
810 .mfg_num = MBCS_MFG_NUM,
815 MODULE_DEVICE_TABLE(cx, mbcs_id_table);
817 static struct cx_drv mbcs_driver = {
819 .id_table = mbcs_id_table,
821 .remove = mbcs_remove,
824 static void __exit mbcs_exit(void)
826 unregister_chrdev(mbcs_major, DEVICE_NAME);
827 cx_driver_unregister(&mbcs_driver);
830 static int __init mbcs_init(void)
834 if (!ia64_platform_is("sn2"))
837 // Put driver into chrdevs[]. Get major number.
838 rv = register_chrdev(mbcs_major, DEVICE_NAME, &mbcs_ops);
840 DBG(KERN_ALERT "mbcs_init: can't get major number. %d\n", rv);
845 return cx_driver_register(&mbcs_driver);
848 module_init(mbcs_init);
849 module_exit(mbcs_exit);
851 MODULE_AUTHOR("Bruce Losure <blosure@sgi.com>");
852 MODULE_DESCRIPTION("Driver for MOATB Core Services");
853 MODULE_LICENSE("GPL");