2 * arch/ia64/sn/io/pciba.c
4 * IRIX PCIBA-inspired user mode PCI interface
8 * device nodes show up in /dev/pci/BB/SS.F (where BB is the bus the
9 * device is on, SS is the slot the device is in, and F is the
10 * device's function on a multi-function card).
12 * when compiled into the kernel, it will only be initialized by the
13 * sgi sn1 specific initialization code. in this case, device nodes
14 * are under /dev/hw/..../
16 * This file is subject to the terms and conditions of the GNU General
17 * Public License. See the file "COPYING" in the main directory of
18 * this archive for more details.
20 * Copyright (C) 2001-2002 Silicon Graphics, Inc. All rights reserved.
22 * 03262001 - Initial version by Chad Talbott
28 register_pci_device should be documented
30 grossness with do_swap should be documented
32 big, gross union'ized node_data should be replaced with independent
35 replace global list of nodes with global lists of resources. could
36 use object oriented approach of allocating and cleaning up
42 #include <linux/config.h>
43 #ifndef CONFIG_DEVFS_FS
44 # error PCIBA requires devfs
47 #include <linux/module.h>
48 #include <linux/devfs_fs_kernel.h>
49 #include <linux/pci.h>
50 #include <linux/list.h>
53 #include <linux/slab.h>
54 #include <linux/vmalloc.h>
55 #include <linux/mman.h>
56 #include <linux/init.h>
57 #include <linux/raw.h>
58 #include <linux/capability.h>
60 #include <asm/uaccess.h>
62 #include <asm/pgalloc.h>
65 #include <asm/sn/pci/pciba.h>
68 MODULE_DESCRIPTION("User mode PCI interface");
69 MODULE_AUTHOR("Chad Talbott");
73 /* #define DEBUG_PCIBA */
76 /* #define TRACE_PCIBA */
78 #if defined(DEBUG_PCIBA)
79 # define DPRINTF(x...) printk(KERN_DEBUG x)
81 # define DPRINTF(x...)
84 #if defined(TRACE_PCIBA)
85 # if defined(__GNUC__)
86 # define TRACE() printk(KERN_DEBUG "%s:%d:%s\n", \
87 __FILE__, __LINE__, __FUNCTION__)
89 # define TRACE() printk(KERN_DEBUG "%s:%d\n", __LINE__, __FILE__)
96 typedef enum { failure, success } status;
97 typedef enum { false, true } boolean;
100 /* major data structures:
104 one for each file registered with devfs. contains everything
105 that any file's fops would need to know about.
107 struct dma_allocation -
109 a single DMA allocation. only the 'dma' nodes care about
110 these. they are there primarily to allow the driver to look
111 up the kernel virtual address of dma buffers allocated by
112 pci_alloc_consistent, as the application is only given the
113 physical address (to program the device's dma, presumably) and
114 cannot supply the kernel virtual address when freeing the
117 it's also useful to maintain a list of buffers allocated
118 through a specific node to allow some sanity checking by this
119 driver. this prevents (for example) a broken application from
120 freeing buffers that it didn't allocate, or buffers allocated
125 a list of all nodes allocated. this allows the driver to free
126 all the memory it has 'kmalloc'd in case of an error, or on
131 a list of all dma buffers allocated by this driver. this
132 allows the driver to 'pci_free_consistent' all buffers on
133 module removal or error.
139 /* flat list of all the device nodes. makes it easy to free
140 them all when we're unregistered */
141 struct list_head global_node_list;
142 devfs_handle_t devfs_handle;
144 void (* cleanup)(struct node_data *);
148 struct pci_dev * dev;
149 struct list_head dma_allocs;
153 struct pci_dev * dev;
154 u32 saved_rom_base_reg;
158 struct resource * res;
161 struct pci_dev * dev;
166 struct dma_allocation {
167 struct list_head list;
175 static LIST_HEAD(global_node_list);
176 static LIST_HEAD(global_dma_list);
179 /* module entry points */
180 int __init pciba_init(void);
181 void __exit pciba_exit(void);
183 static status __init register_with_devfs(void);
184 static void __exit unregister_with_devfs(void);
186 static status __init register_pci_device(devfs_handle_t device_dir_handle,
187 struct pci_dev * dev);
189 /* file operations */
190 static int generic_open(struct inode * inode, struct file * file);
191 static int rom_mmap(struct file * file, struct vm_area_struct * vma);
192 static int rom_release(struct inode * inode, struct file * file);
193 static int base_mmap(struct file * file, struct vm_area_struct * vma);
194 static int config_ioctl(struct inode * inode, struct file * file,
197 static int dma_ioctl(struct inode * inode, struct file * file,
200 static int dma_mmap(struct file * file, struct vm_area_struct * vma);
202 /* support routines */
203 static int mmap_pci_address(struct vm_area_struct * vma, unsigned long pci_va);
204 static int mmap_kernel_address(struct vm_area_struct * vma, void * kernel_va);
207 static void dump_nodes(struct list_head * nodes);
208 static void dump_allocations(struct list_head * dalp);
211 /* file operations for each type of node */
212 static struct file_operations rom_fops = {
220 static struct file_operations base_fops = {
227 static struct file_operations config_fops = {
233 static struct file_operations dma_fops = {
241 module_init(pciba_init);
242 module_exit(pciba_exit);
250 if (register_with_devfs() == failure)
251 return 1; /* failure */
253 printk("PCIBA (a user mode PCI interface) initialized.\n");
255 return 0; /* success */
264 /* FIXME: should also free all that memory that we allocated
266 unregister_with_devfs();
274 struct node_data * nd;
278 list_for_each(nd, &node_list) {
279 kfree(list_entry(nd, struct nd, node_list));
284 #if !defined(CONFIG_IA64_SGI_SN1)
287 register_with_devfs(void)
289 struct pci_dev * dev;
290 devfs_handle_t device_dir_handle;
295 if (!devfs_mk_dir(NULL, "pci", NULL))
298 /* FIXME: don't forget /dev/pci/mem & /dev/pci/io */
300 pci_for_each_dev(dev) {
301 sprintf(devfs_path, "pci/%02x/%02x.%x",
303 PCI_SLOT(dev->devfn),
304 PCI_FUNC(dev->devfn));
307 devfs_mk_dir(NULL, devfs_path, NULL);
308 if (device_dir_handle == NULL)
311 if (register_pci_device(device_dir_handle, dev) == failure) {
322 extern devfs_handle_t
323 devfn_to_vertex(unsigned char busnum, unsigned int devfn);
326 register_with_devfs(void)
328 struct pci_dev * dev;
329 devfs_handle_t device_dir_handle;
333 /* FIXME: don't forget /dev/.../pci/mem & /dev/.../pci/io */
335 pci_for_each_dev(dev) {
336 device_dir_handle = devfn_to_vertex(dev->bus->number,
338 if (device_dir_handle == NULL)
341 if (register_pci_device(device_dir_handle, dev) == failure) {
351 unregister_with_devfs(void)
353 struct list_head * lhp;
354 struct node_data * nd;
358 list_for_each(lhp, &global_node_list) {
359 nd = list_entry(lhp, struct node_data, global_node_list);
360 devfs_unregister(nd->devfs_handle);
366 struct node_data * new_node(void)
368 struct node_data * node;
372 node = kmalloc(sizeof(struct node_data), GFP_KERNEL);
375 list_add(&node->global_node_list, &global_node_list);
380 void dma_cleanup(struct node_data * dma_node)
384 /* FIXME: should free these allocations */
386 dump_allocations(&dma_node->u.dma.dma_allocs);
388 devfs_unregister(dma_node->devfs_handle);
392 void init_dma_node(struct node_data * node,
393 struct pci_dev * dev, devfs_handle_t dh)
397 node->devfs_handle = dh;
398 node->u.dma.dev = dev;
399 node->cleanup = dma_cleanup;
400 INIT_LIST_HEAD(&node->u.dma.dma_allocs);
404 void rom_cleanup(struct node_data * rom_node)
408 if (rom_node->u.rom.mmapped)
409 pci_write_config_dword(rom_node->u.rom.dev,
411 rom_node->u.rom.saved_rom_base_reg);
412 devfs_unregister(rom_node->devfs_handle);
416 void init_rom_node(struct node_data * node,
417 struct pci_dev * dev, devfs_handle_t dh)
421 node->devfs_handle = dh;
422 node->u.rom.dev = dev;
423 node->cleanup = rom_cleanup;
424 node->u.rom.mmapped = false;
429 register_pci_device(devfs_handle_t device_dir_handle, struct pci_dev * dev)
431 struct node_data * nd;
433 devfs_handle_t node_devfs_handle;
439 /* register nodes for all the device's base address registers */
440 for (ri = 0; ri < PCI_ROM_RESOURCE; ri++) {
441 if (pci_resource_len(dev, ri) != 0) {
442 sprintf(devfs_path, "base/%d", ri);
443 if (devfs_register(device_dir_handle, devfs_path,
446 S_IFREG | S_IRUSR | S_IWUSR,
448 &dev->resource[ri]) == NULL)
453 /* register a node corresponding to the first MEM resource on
455 for (ri = 0; ri < PCI_ROM_RESOURCE; ri++) {
456 if (dev->resource[ri].flags & IORESOURCE_MEM &&
457 pci_resource_len(dev, ri) != 0) {
458 if (devfs_register(device_dir_handle, "mem",
460 S_IFREG | S_IRUSR | S_IWUSR,
462 &dev->resource[ri]) == NULL)
468 /* also register a node corresponding to the first IO resource
470 for (ri = 0; ri < PCI_ROM_RESOURCE; ri++) {
471 if (dev->resource[ri].flags & IORESOURCE_IO &&
472 pci_resource_len(dev, ri) != 0) {
473 if (devfs_register(device_dir_handle, "io",
475 S_IFREG | S_IRUSR | S_IWUSR,
477 &dev->resource[ri]) == NULL)
483 /* register a node corresponding to the device's ROM resource,
485 if (pci_resource_len(dev, PCI_ROM_RESOURCE) != 0) {
489 node_devfs_handle = devfs_register(device_dir_handle, "rom",
493 if (node_devfs_handle == NULL)
495 init_rom_node(nd, dev, node_devfs_handle);
498 /* register a node that allows ioctl's to read and write to
499 the device's config space */
500 if (devfs_register(device_dir_handle, "config", DEVFS_FL_NONE,
501 0, 0, S_IFREG | S_IRUSR | S_IWUSR,
502 &config_fops, dev) == NULL)
506 /* finally, register a node that allows ioctl's to allocate
507 and free DMA buffers, as well as memory map those
513 devfs_register(device_dir_handle, "dma", DEVFS_FL_NONE,
514 0, 0, S_IFREG | S_IRUSR | S_IWUSR,
516 if (node_devfs_handle == NULL)
518 init_dma_node(nd, dev, node_devfs_handle);
521 dump_nodes(&global_node_list);
529 generic_open(struct inode * inode, struct file * file)
533 /* FIXME: should check that they're not trying to open the ROM
536 return 0; /* success */
541 rom_mmap(struct file * file, struct vm_area_struct * vma)
543 unsigned long pci_pa;
544 struct node_data * nd;
548 nd = (struct node_data * )file->private_data;
550 pci_pa = pci_resource_start(nd->u.rom.dev, PCI_ROM_RESOURCE);
552 if (!nd->u.rom.mmapped) {
553 nd->u.rom.mmapped = true;
554 DPRINTF("Enabling ROM address decoder.\n");
556 "rom_mmap: FIXME: some cards do not allow both ROM and memory addresses to\n"
557 "rom_mmap: FIXME: be enabled simultaneously, as they share a decoder.\n");
558 pci_read_config_dword(nd->u.rom.dev, PCI_ROM_ADDRESS,
559 &nd->u.rom.saved_rom_base_reg);
560 DPRINTF("ROM base address contains %x\n",
561 nd->u.rom.saved_rom_base_reg);
562 pci_write_config_dword(nd->u.rom.dev, PCI_ROM_ADDRESS,
563 nd->u.rom.saved_rom_base_reg |
564 PCI_ROM_ADDRESS_ENABLE);
567 return mmap_pci_address(vma, pci_pa);
572 rom_release(struct inode * inode, struct file * file)
574 struct node_data * nd;
578 nd = (struct node_data * )file->private_data;
580 if (nd->u.rom.mmapped) {
581 nd->u.rom.mmapped = false;
582 DPRINTF("Disabling ROM address decoder.\n");
583 pci_write_config_dword(nd->u.rom.dev, PCI_ROM_ADDRESS,
584 nd->u.rom.saved_rom_base_reg);
586 return 0; /* indicate success */
591 base_mmap(struct file * file, struct vm_area_struct * vma)
593 struct resource * resource;
597 resource = (struct resource *)file->private_data;
599 return mmap_pci_address(vma, resource->start);
604 config_ioctl(struct inode * inode, struct file * file,
608 struct pci_dev * dev;
614 } read_data, write_data;
616 int dir, size, offset;
620 DPRINTF("cmd = %x (DIR = %x, TYPE = %x, NR = %x, SIZE = %x)\n",
622 _IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
623 DPRINTF("arg = %lx\n", arg);
625 dev = (struct pci_dev *)file->private_data;
627 /* PCIIOCCFG{RD,WR}: read and/or write PCI configuration
628 space. If both, the read happens first (this becomes a swap
629 operation, atomic with respect to other updates through
634 #define do_swap(suffix, type) \
636 if (dir & _IOC_READ) { \
637 pci_read_config_##suffix(dev, _IOC_NR(cmd), \
638 &read_data.suffix); \
640 if (dir & _IOC_WRITE) { \
641 get_user(write_data.suffix, (type)arg); \
642 pci_write_config_##suffix(dev, _IOC_NR(cmd), \
643 write_data.suffix); \
645 if (dir & _IOC_READ) { \
646 put_user(read_data.suffix, (type)arg); \
650 size = _IOC_SIZE(cmd);
651 offset = _IOC_NR(cmd);
653 DPRINTF("sanity check\n");
654 if (((size > 0) || (size <= 4)) &&
655 ((offset + size) <= 256) &&
656 (dir & (_IOC_READ | _IOC_WRITE))) {
661 do_swap(byte, uint8_t *);
664 do_swap(word, uint16_t *);
667 do_swap(dword, uint32_t *);
670 DPRINTF("invalid ioctl\n");
682 dump_allocations(struct list_head * dalp)
684 struct dma_allocation * dap;
685 struct list_head * p;
688 list_for_each(p, dalp) {
689 dap = list_entry(p, struct dma_allocation,
691 printk(" handle = %lx, va = %p\n",
692 dap->handle, dap->va);
698 dump_nodes(struct list_head * nodes)
700 struct node_data * ndp;
701 struct list_head * p;
704 list_for_each(p, nodes) {
705 ndp = list_entry(p, struct node_data,
707 printk(" %p\n", (void *)ndp);
714 #define NEW(ptr) (ptr = kmalloc(sizeof (*(ptr)), GFP_KERNEL))
722 for (i = 0; i < 5; i++) {
723 struct dma_allocation * new_alloc;
725 new_alloc->va = (void *)i;
726 new_alloc->handle = 5*i;
727 printk("%d - the_list->next = %lx\n", i, the_list.next);
728 list_add(&new_alloc->list, &the_list);
730 dump_allocations(&the_list);
736 static LIST_HEAD(dma_buffer_list);
740 dma_ioctl(struct inode * inode, struct file * file,
744 struct node_data * nd;
747 struct dma_allocation * dma_alloc;
748 struct list_head * iterp;
752 DPRINTF("cmd = %x\n", cmd);
753 DPRINTF("arg = %lx\n", arg);
755 nd = (struct node_data *)file->private_data;
758 DPRINTF("at dma_ioctl entry\n");
759 dump_allocations(&nd->u.dma.dma_allocs);
764 /* PCIIOCDMAALLOC: allocate a chunk of physical memory
765 and set it up for DMA. Return the PCI address that
767 DPRINTF("case PCIIOCDMAALLOC (%lx)\n", PCIIOCDMAALLOC);
769 if ( (result = get_user(argv, (uint64_t *)arg)) )
771 DPRINTF("argv (size of buffer) = %lx\n", argv);
773 dma_alloc = (struct dma_allocation *)
774 kmalloc(sizeof(struct dma_allocation), GFP_KERNEL);
775 if (dma_alloc == NULL)
778 dma_alloc->size = (size_t)argv;
779 dma_alloc->va = pci_alloc_consistent(nd->u.dma.dev,
782 DPRINTF("dma_alloc->va = %p, dma_alloc->handle = %lx\n",
783 dma_alloc->va, dma_alloc->handle);
784 if (dma_alloc->va == NULL) {
789 list_add(&dma_alloc->list, &nd->u.dma.dma_allocs);
790 if ( (result = put_user((uint64_t)dma_alloc->handle,
791 (uint64_t *)arg)) ) {
792 DPRINTF("put_user failed\n");
793 pci_free_consistent(nd->u.dma.dev, (size_t)argv,
794 dma_alloc->va, dma_alloc->handle);
800 DPRINTF("after insertion\n");
801 dump_allocations(&nd->u.dma.dma_allocs);
806 DPRINTF("case PCIIOCDMAFREE (%lx)\n", PCIIOCDMAFREE);
808 if ( (result = get_user(argv, (uint64_t *)arg)) ) {
809 DPRINTF("get_user failed\n");
813 DPRINTF("argv (physical address of DMA buffer) = %lx\n", argv);
814 list_for_each(iterp, &nd->u.dma.dma_allocs) {
815 struct dma_allocation * da =
816 list_entry(iterp, struct dma_allocation, list);
817 if (da->handle == argv) {
818 pci_free_consistent(nd->u.dma.dev, da->size,
823 DPRINTF("after deletion\n");
824 dump_allocations(&nd->u.dma.dma_allocs);
826 return 0; /* success */
829 /* previously allocated dma buffer wasn't found */
830 DPRINTF("attempt to free invalid dma handle\n");
834 DPRINTF("undefined ioctl\n");
838 DPRINTF("success\n");
844 dma_mmap(struct file * file, struct vm_area_struct * vma)
846 struct node_data * nd;
847 struct list_head * iterp;
852 nd = (struct node_data *)file->private_data;
854 DPRINTF("vma->vm_start is %lx\n", vma->vm_start);
855 DPRINTF("vma->vm_end is %lx\n", vma->vm_end);
856 DPRINTF("offset = %lx\n", vma->vm_pgoff);
858 /* get kernel virtual address for the dma buffer (necessary
860 list_for_each(iterp, &nd->u.dma.dma_allocs) {
861 struct dma_allocation * da =
862 list_entry(iterp, struct dma_allocation, list);
863 /* why does mmap shift its offset argument? */
864 if (da->handle == vma->vm_pgoff << PAGE_SHIFT) {
865 DPRINTF("found dma handle\n");
866 if ( (result = mmap_kernel_address(vma,
868 return result; /* failure */
870 /* it seems like at least one of these
871 should show up in user land....
872 I'm missing something */
873 *(char *)da->va = 0xaa;
874 strncpy(da->va, " Toastie!", da->size);
875 if (put_user(0x18badbeeful,
876 (u64 *)vma->vm_start))
877 DPRINTF("put_user failed?!\n");
878 return 0; /* success */
883 DPRINTF("attempt to mmap an invalid dma handle\n");
889 mmap_pci_address(struct vm_area_struct * vma, unsigned long pci_va)
891 unsigned long pci_pa;
895 DPRINTF("vma->vm_start is %lx\n", vma->vm_start);
896 DPRINTF("vma->vm_end is %lx\n", vma->vm_end);
898 /* the size of the vma doesn't necessarily correspond to the
899 size specified in the mmap call. So we can't really do any
900 kind of sanity check here. This is a dangerous driver, and
901 it's very easy for a user process to kill the machine. */
903 DPRINTF("PCI base at virtual address %lx\n", pci_va);
904 /* the __pa macro is intended for region 7 on IA64, so it
905 doesn't work for region 6 */
906 /* pci_pa = __pa(pci_va); */
907 /* should be replaced by __tpa or equivalent (preferably a
908 generic equivalent) */
909 pci_pa = pci_va & ~0xe000000000000000ul;
910 DPRINTF("PCI base at physical address %lx\n", pci_pa);
912 /* there are various arch-specific versions of this function
913 defined in linux/drivers/char/mem.c, but it would be nice
914 if all architectures put it in pgtable.h. it's defined
915 there for ia64.... */
916 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
918 vma->vm_flags |= VM_NONCACHED | VM_RESERVED | VM_IO;
920 return io_remap_page_range(vma->vm_start, pci_pa,
921 vma->vm_end-vma->vm_start,
927 mmap_kernel_address(struct vm_area_struct * vma, void * kernel_va)
929 unsigned long kernel_pa;
933 DPRINTF("vma->vm_start is %lx\n", vma->vm_start);
934 DPRINTF("vma->vm_end is %lx\n", vma->vm_end);
936 /* the size of the vma doesn't necessarily correspond to the
937 size specified in the mmap call. So we can't really do any
938 kind of sanity check here. This is a dangerous driver, and
939 it's very easy for a user process to kill the machine. */
941 DPRINTF("mapping virtual address %p\n", kernel_va);
942 kernel_pa = __pa(kernel_va);
943 DPRINTF("mapping physical address %lx\n", kernel_pa);
945 vma->vm_flags |= VM_NONCACHED | VM_RESERVED | VM_IO;
947 return remap_page_range(vma->vm_start, kernel_pa,
948 vma->vm_end-vma->vm_start,