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 * @kobj: kobject for mapping
57 * @attr: struct bin_attribute for the file being mapped
58 * @vma: struct vm_area_struct passed into the mmap
59 * @sparse: address space type
61 * Use the bus mapping routines to map a PCI resource into userspace.
63 static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
64 struct vm_area_struct *vma, int sparse)
66 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
67 struct device, kobj));
68 struct resource *res = (struct resource *)attr->private;
69 enum pci_mmap_state mmap_type;
70 struct pci_bus_region bar;
73 for (i = 0; i < PCI_ROM_RESOURCE; i++)
74 if (res == &pdev->resource[i])
76 if (i >= PCI_ROM_RESOURCE)
79 if (!__pci_mmap_fits(pdev, i, vma, sparse))
82 if (iomem_is_exclusive(res->start))
85 pcibios_resource_to_bus(pdev, &bar, res);
86 vma->vm_pgoff += bar.start >> (PAGE_SHIFT - (sparse ? 5 : 0));
87 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
89 return hose_mmap_page_range(pdev->sysdata, vma, mmap_type, sparse);
92 static int pci_mmap_resource_sparse(struct kobject *kobj,
93 struct bin_attribute *attr,
94 struct vm_area_struct *vma)
96 return pci_mmap_resource(kobj, attr, vma, 1);
99 static int pci_mmap_resource_dense(struct kobject *kobj,
100 struct bin_attribute *attr,
101 struct vm_area_struct *vma)
103 return pci_mmap_resource(kobj, attr, vma, 0);
107 * pci_remove_resource_files - cleanup resource files
108 * @dev: dev to cleanup
110 * If we created resource files for @dev, remove them from sysfs and
111 * free their resources.
113 void pci_remove_resource_files(struct pci_dev *pdev)
117 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
118 struct bin_attribute *res_attr;
120 res_attr = pdev->res_attr[i];
122 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
126 res_attr = pdev->res_attr_wc[i];
128 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
134 static int sparse_mem_mmap_fits(struct pci_dev *pdev, int num)
136 struct pci_bus_region bar;
137 struct pci_controller *hose = pdev->sysdata;
139 unsigned long sparse_size;
141 pcibios_resource_to_bus(pdev, &bar, &pdev->resource[num]);
143 /* All core logic chips have 4G sparse address space, except
144 CIA which has 16G (see xxx_SPARSE_MEM and xxx_DENSE_MEM
145 definitions in asm/core_xxx.h files). This corresponds
146 to 128M or 512M of the bus space. */
147 dense_offset = (long)(hose->dense_mem_base - hose->sparse_mem_base);
148 sparse_size = dense_offset >= 0x400000000UL ? 0x20000000 : 0x8000000;
150 return bar.end < sparse_size;
153 static int pci_create_one_attr(struct pci_dev *pdev, int num, char *name,
154 char *suffix, struct bin_attribute *res_attr,
155 unsigned long sparse)
157 size_t size = pci_resource_len(pdev, num);
159 sprintf(name, "resource%d%s", num, suffix);
160 res_attr->mmap = sparse ? pci_mmap_resource_sparse :
161 pci_mmap_resource_dense;
162 res_attr->attr.name = name;
163 res_attr->attr.mode = S_IRUSR | S_IWUSR;
164 res_attr->size = sparse ? size << 5 : size;
165 res_attr->private = &pdev->resource[num];
166 return sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
169 static int pci_create_attr(struct pci_dev *pdev, int num)
171 /* allocate attribute structure, piggyback attribute name */
172 int retval, nlen1, nlen2 = 0, res_count = 1;
173 unsigned long sparse_base, dense_base;
174 struct bin_attribute *attr;
175 struct pci_controller *hose = pdev->sysdata;
176 char *suffix, *attr_name;
178 suffix = ""; /* Assume bwx machine, normal resourceN files. */
181 if (pdev->resource[num].flags & IORESOURCE_MEM) {
182 sparse_base = hose->sparse_mem_base;
183 dense_base = hose->dense_mem_base;
184 if (sparse_base && !sparse_mem_mmap_fits(pdev, num)) {
187 nlen1 = 16; /* resourceN_dense */
190 sparse_base = hose->sparse_io_base;
191 dense_base = hose->dense_io_base;
198 nlen2 = 16; /* resourceN_dense */
203 attr = kzalloc(sizeof(*attr) * res_count + nlen1 + nlen2, GFP_ATOMIC);
207 /* Create bwx, sparse or single dense file */
208 attr_name = (char *)(attr + res_count);
209 pdev->res_attr[num] = attr;
210 retval = pci_create_one_attr(pdev, num, attr_name, suffix, attr,
212 if (retval || res_count == 1)
215 /* Create dense file */
218 pdev->res_attr_wc[num] = attr;
219 return pci_create_one_attr(pdev, num, attr_name, "_dense", attr, 0);
223 * pci_create_resource_files - create resource files in sysfs for @dev
224 * @dev: dev in question
226 * Walk the resources in @dev creating files for each resource available.
228 int pci_create_resource_files(struct pci_dev *pdev)
233 /* Expose the PCI resources from this device as files */
234 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
236 /* skip empty resources */
237 if (!pci_resource_len(pdev, i))
240 retval = pci_create_attr(pdev, i);
242 pci_remove_resource_files(pdev);
249 /* Legacy I/O bus mapping stuff. */
251 static int __legacy_mmap_fits(struct pci_controller *hose,
252 struct vm_area_struct *vma,
253 unsigned long res_size, int sparse)
255 unsigned long nr, start, size;
257 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
258 start = vma->vm_pgoff;
259 size = ((res_size - 1) >> PAGE_SHIFT) + 1;
261 if (start < size && size - start >= nr)
263 WARN(1, "process \"%s\" tried to map%s 0x%08lx-0x%08lx on hose %d "
265 current->comm, sparse ? " sparse" : "", start, start + nr,
270 static inline int has_sparse(struct pci_controller *hose,
271 enum pci_mmap_state mmap_type)
275 base = (mmap_type == pci_mmap_mem) ? hose->sparse_mem_base :
276 hose->sparse_io_base;
281 int pci_mmap_legacy_page_range(struct pci_bus *bus, struct vm_area_struct *vma,
282 enum pci_mmap_state mmap_type)
284 struct pci_controller *hose = bus->sysdata;
285 int sparse = has_sparse(hose, mmap_type);
286 unsigned long res_size;
288 res_size = (mmap_type == pci_mmap_mem) ? bus->legacy_mem->size :
289 bus->legacy_io->size;
290 if (!__legacy_mmap_fits(hose, vma, res_size, sparse))
293 return hose_mmap_page_range(hose, vma, mmap_type, sparse);
297 * pci_adjust_legacy_attr - adjustment of legacy file attributes
298 * @b: bus to create files under
299 * @mmap_type: I/O port or memory
301 * Adjust file name and size for sparse mappings.
303 void pci_adjust_legacy_attr(struct pci_bus *bus, enum pci_mmap_state mmap_type)
305 struct pci_controller *hose = bus->sysdata;
307 if (!has_sparse(hose, mmap_type))
310 if (mmap_type == pci_mmap_mem) {
311 bus->legacy_mem->attr.name = "legacy_mem_sparse";
312 bus->legacy_mem->size <<= 5;
314 bus->legacy_io->attr.name = "legacy_io_sparse";
315 bus->legacy_io->size <<= 5;
320 /* Legacy I/O bus read/write functions */
321 int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
323 struct pci_controller *hose = bus->sysdata;
325 port += hose->io_space->start;
329 *((u8 *)val) = inb(port);
334 *((u16 *)val) = inw(port);
339 *((u32 *)val) = inl(port);
345 int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
347 struct pci_controller *hose = bus->sysdata;
349 port += hose->io_space->start;