2 * arch/alpha/kernel/pci-sysfs.c
4 * Copyright (C) 2009 Ivan Kokshaysky
6 * Alpha PCI resource files.
8 * Loosely based on generic HAVE_PCI_MMAP implementation in
9 * drivers/pci/pci-sysfs.c
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/pci.h>
16 static int hose_mmap_page_range(struct pci_controller *hose,
17 struct vm_area_struct *vma,
18 enum pci_mmap_state mmap_type, int sparse)
22 if (mmap_type == pci_mmap_mem)
23 base = sparse ? hose->sparse_mem_base : hose->dense_mem_base;
25 base = sparse ? hose->sparse_io_base : hose->dense_io_base;
27 vma->vm_pgoff += base >> PAGE_SHIFT;
28 vma->vm_flags |= (VM_IO | VM_RESERVED);
30 return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
31 vma->vm_end - vma->vm_start,
35 static int __pci_mmap_fits(struct pci_dev *pdev, int num,
36 struct vm_area_struct *vma, int sparse)
38 unsigned long nr, start, size;
39 int shift = sparse ? 5 : 0;
41 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
42 start = vma->vm_pgoff;
43 size = ((pci_resource_len(pdev, num) - 1) >> (PAGE_SHIFT - shift)) + 1;
45 if (start < size && size - start >= nr)
47 WARN(1, "process \"%s\" tried to map%s 0x%08lx-0x%08lx on %s BAR %d "
49 current->comm, sparse ? " sparse" : "", start, start + nr,
50 pci_name(pdev), num, size);
55 * pci_mmap_resource - map a PCI resource into user memory space
56 * @filp: open sysfs file
57 * @kobj: kobject for mapping
58 * @attr: struct bin_attribute for the file being mapped
59 * @vma: struct vm_area_struct passed into the mmap
60 * @sparse: address space type
62 * Use the bus mapping routines to map a PCI resource into userspace.
64 static int pci_mmap_resource(struct file *filp, struct kobject *kobj,
65 struct bin_attribute *attr,
66 struct vm_area_struct *vma, int sparse)
68 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
69 struct device, kobj));
70 struct resource *res = (struct resource *)attr->private;
71 enum pci_mmap_state mmap_type;
72 struct pci_bus_region bar;
75 for (i = 0; i < PCI_ROM_RESOURCE; i++)
76 if (res == &pdev->resource[i])
78 if (i >= PCI_ROM_RESOURCE)
81 if (!__pci_mmap_fits(pdev, i, vma, sparse))
84 if (iomem_is_exclusive(res->start))
87 pcibios_resource_to_bus(pdev, &bar, res);
88 vma->vm_pgoff += bar.start >> (PAGE_SHIFT - (sparse ? 5 : 0));
89 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
91 return hose_mmap_page_range(pdev->sysdata, vma, mmap_type, sparse);
94 static int pci_mmap_resource_sparse(struct file *filp, struct kobject *kobj,
95 struct bin_attribute *attr,
96 struct vm_area_struct *vma)
98 return pci_mmap_resource(kobj, attr, vma, 1);
101 static int pci_mmap_resource_dense(struct file *filp, struct kobject *kobj,
102 struct bin_attribute *attr,
103 struct vm_area_struct *vma)
105 return pci_mmap_resource(kobj, attr, vma, 0);
109 * pci_remove_resource_files - cleanup resource files
110 * @dev: dev to cleanup
112 * If we created resource files for @dev, remove them from sysfs and
113 * free their resources.
115 void pci_remove_resource_files(struct pci_dev *pdev)
119 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
120 struct bin_attribute *res_attr;
122 res_attr = pdev->res_attr[i];
124 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
128 res_attr = pdev->res_attr_wc[i];
130 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
136 static int sparse_mem_mmap_fits(struct pci_dev *pdev, int num)
138 struct pci_bus_region bar;
139 struct pci_controller *hose = pdev->sysdata;
141 unsigned long sparse_size;
143 pcibios_resource_to_bus(pdev, &bar, &pdev->resource[num]);
145 /* All core logic chips have 4G sparse address space, except
146 CIA which has 16G (see xxx_SPARSE_MEM and xxx_DENSE_MEM
147 definitions in asm/core_xxx.h files). This corresponds
148 to 128M or 512M of the bus space. */
149 dense_offset = (long)(hose->dense_mem_base - hose->sparse_mem_base);
150 sparse_size = dense_offset >= 0x400000000UL ? 0x20000000 : 0x8000000;
152 return bar.end < sparse_size;
155 static int pci_create_one_attr(struct pci_dev *pdev, int num, char *name,
156 char *suffix, struct bin_attribute *res_attr,
157 unsigned long sparse)
159 size_t size = pci_resource_len(pdev, num);
161 sprintf(name, "resource%d%s", num, suffix);
162 res_attr->mmap = sparse ? pci_mmap_resource_sparse :
163 pci_mmap_resource_dense;
164 res_attr->attr.name = name;
165 res_attr->attr.mode = S_IRUSR | S_IWUSR;
166 res_attr->size = sparse ? size << 5 : size;
167 res_attr->private = &pdev->resource[num];
168 return sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
171 static int pci_create_attr(struct pci_dev *pdev, int num)
173 /* allocate attribute structure, piggyback attribute name */
174 int retval, nlen1, nlen2 = 0, res_count = 1;
175 unsigned long sparse_base, dense_base;
176 struct bin_attribute *attr;
177 struct pci_controller *hose = pdev->sysdata;
178 char *suffix, *attr_name;
180 suffix = ""; /* Assume bwx machine, normal resourceN files. */
183 if (pdev->resource[num].flags & IORESOURCE_MEM) {
184 sparse_base = hose->sparse_mem_base;
185 dense_base = hose->dense_mem_base;
186 if (sparse_base && !sparse_mem_mmap_fits(pdev, num)) {
189 nlen1 = 16; /* resourceN_dense */
192 sparse_base = hose->sparse_io_base;
193 dense_base = hose->dense_io_base;
200 nlen2 = 16; /* resourceN_dense */
205 attr = kzalloc(sizeof(*attr) * res_count + nlen1 + nlen2, GFP_ATOMIC);
209 /* Create bwx, sparse or single dense file */
210 attr_name = (char *)(attr + res_count);
211 pdev->res_attr[num] = attr;
212 retval = pci_create_one_attr(pdev, num, attr_name, suffix, attr,
214 if (retval || res_count == 1)
217 /* Create dense file */
220 pdev->res_attr_wc[num] = attr;
221 return pci_create_one_attr(pdev, num, attr_name, "_dense", attr, 0);
225 * pci_create_resource_files - create resource files in sysfs for @dev
226 * @dev: dev in question
228 * Walk the resources in @dev creating files for each resource available.
230 int pci_create_resource_files(struct pci_dev *pdev)
235 /* Expose the PCI resources from this device as files */
236 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
238 /* skip empty resources */
239 if (!pci_resource_len(pdev, i))
242 retval = pci_create_attr(pdev, i);
244 pci_remove_resource_files(pdev);
251 /* Legacy I/O bus mapping stuff. */
253 static int __legacy_mmap_fits(struct pci_controller *hose,
254 struct vm_area_struct *vma,
255 unsigned long res_size, int sparse)
257 unsigned long nr, start, size;
259 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
260 start = vma->vm_pgoff;
261 size = ((res_size - 1) >> PAGE_SHIFT) + 1;
263 if (start < size && size - start >= nr)
265 WARN(1, "process \"%s\" tried to map%s 0x%08lx-0x%08lx on hose %d "
267 current->comm, sparse ? " sparse" : "", start, start + nr,
272 static inline int has_sparse(struct pci_controller *hose,
273 enum pci_mmap_state mmap_type)
277 base = (mmap_type == pci_mmap_mem) ? hose->sparse_mem_base :
278 hose->sparse_io_base;
283 int pci_mmap_legacy_page_range(struct pci_bus *bus, struct vm_area_struct *vma,
284 enum pci_mmap_state mmap_type)
286 struct pci_controller *hose = bus->sysdata;
287 int sparse = has_sparse(hose, mmap_type);
288 unsigned long res_size;
290 res_size = (mmap_type == pci_mmap_mem) ? bus->legacy_mem->size :
291 bus->legacy_io->size;
292 if (!__legacy_mmap_fits(hose, vma, res_size, sparse))
295 return hose_mmap_page_range(hose, vma, mmap_type, sparse);
299 * pci_adjust_legacy_attr - adjustment of legacy file attributes
300 * @b: bus to create files under
301 * @mmap_type: I/O port or memory
303 * Adjust file name and size for sparse mappings.
305 void pci_adjust_legacy_attr(struct pci_bus *bus, enum pci_mmap_state mmap_type)
307 struct pci_controller *hose = bus->sysdata;
309 if (!has_sparse(hose, mmap_type))
312 if (mmap_type == pci_mmap_mem) {
313 bus->legacy_mem->attr.name = "legacy_mem_sparse";
314 bus->legacy_mem->size <<= 5;
316 bus->legacy_io->attr.name = "legacy_io_sparse";
317 bus->legacy_io->size <<= 5;
322 /* Legacy I/O bus read/write functions */
323 int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
325 struct pci_controller *hose = bus->sysdata;
327 port += hose->io_space->start;
331 *((u8 *)val) = inb(port);
336 *((u16 *)val) = inw(port);
341 *((u32 *)val) = inl(port);
347 int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
349 struct pci_controller *hose = bus->sysdata;
351 port += hose->io_space->start;