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/pci.h>
15 static int hose_mmap_page_range(struct pci_controller *hose,
16 struct vm_area_struct *vma,
17 enum pci_mmap_state mmap_type, int sparse)
21 if (mmap_type == pci_mmap_mem)
22 base = sparse ? hose->sparse_mem_base : hose->dense_mem_base;
24 base = sparse ? hose->sparse_io_base : hose->dense_io_base;
26 vma->vm_pgoff += base >> PAGE_SHIFT;
27 vma->vm_flags |= (VM_IO | VM_RESERVED);
29 return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
30 vma->vm_end - vma->vm_start,
34 static int __pci_mmap_fits(struct pci_dev *pdev, int num,
35 struct vm_area_struct *vma, int sparse)
37 unsigned long nr, start, size;
38 int shift = sparse ? 5 : 0;
40 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
41 start = vma->vm_pgoff;
42 size = ((pci_resource_len(pdev, num) - 1) >> (PAGE_SHIFT - shift)) + 1;
44 if (start < size && size - start >= nr)
46 WARN(1, "process \"%s\" tried to map%s 0x%08lx-0x%08lx on %s BAR %d "
48 current->comm, sparse ? " sparse" : "", start, start + nr,
49 pci_name(pdev), num, size);
54 * pci_mmap_resource - map a PCI resource into user memory space
55 * @kobj: kobject for mapping
56 * @attr: struct bin_attribute for the file being mapped
57 * @vma: struct vm_area_struct passed into the mmap
58 * @sparse: address space type
60 * Use the bus mapping routines to map a PCI resource into userspace.
62 static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
63 struct vm_area_struct *vma, int sparse)
65 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
66 struct device, kobj));
67 struct resource *res = (struct resource *)attr->private;
68 enum pci_mmap_state mmap_type;
69 struct pci_bus_region bar;
72 for (i = 0; i < PCI_ROM_RESOURCE; i++)
73 if (res == &pdev->resource[i])
75 if (i >= PCI_ROM_RESOURCE)
78 if (!__pci_mmap_fits(pdev, i, vma, sparse))
81 if (iomem_is_exclusive(res->start))
84 pcibios_resource_to_bus(pdev, &bar, res);
85 vma->vm_pgoff += bar.start >> (PAGE_SHIFT - (sparse ? 5 : 0));
86 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
88 return hose_mmap_page_range(pdev->sysdata, vma, mmap_type, sparse);
91 static int pci_mmap_resource_sparse(struct kobject *kobj,
92 struct bin_attribute *attr,
93 struct vm_area_struct *vma)
95 return pci_mmap_resource(kobj, attr, vma, 1);
98 static int pci_mmap_resource_dense(struct kobject *kobj,
99 struct bin_attribute *attr,
100 struct vm_area_struct *vma)
102 return pci_mmap_resource(kobj, attr, vma, 0);
106 * pci_remove_resource_files - cleanup resource files
107 * @dev: dev to cleanup
109 * If we created resource files for @dev, remove them from sysfs and
110 * free their resources.
112 void pci_remove_resource_files(struct pci_dev *pdev)
116 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
117 struct bin_attribute *res_attr;
119 res_attr = pdev->res_attr[i];
121 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
125 res_attr = pdev->res_attr_wc[i];
127 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
133 static int sparse_mem_mmap_fits(struct pci_dev *pdev, int num)
135 struct pci_bus_region bar;
136 struct pci_controller *hose = pdev->sysdata;
138 unsigned long sparse_size;
140 pcibios_resource_to_bus(pdev, &bar, &pdev->resource[num]);
142 /* All core logic chips have 4G sparse address space, except
143 CIA which has 16G (see xxx_SPARSE_MEM and xxx_DENSE_MEM
144 definitions in asm/core_xxx.h files). This corresponds
145 to 128M or 512M of the bus space. */
146 dense_offset = (long)(hose->dense_mem_base - hose->sparse_mem_base);
147 sparse_size = dense_offset >= 0x400000000UL ? 0x20000000 : 0x8000000;
149 return bar.end < sparse_size;
152 static int pci_create_one_attr(struct pci_dev *pdev, int num, char *name,
153 char *suffix, struct bin_attribute *res_attr,
154 unsigned long sparse)
156 size_t size = pci_resource_len(pdev, num);
158 sprintf(name, "resource%d%s", num, suffix);
159 res_attr->mmap = sparse ? pci_mmap_resource_sparse :
160 pci_mmap_resource_dense;
161 res_attr->attr.name = name;
162 res_attr->attr.mode = S_IRUSR | S_IWUSR;
163 res_attr->size = sparse ? size << 5 : size;
164 res_attr->private = &pdev->resource[num];
165 return sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
168 static int pci_create_attr(struct pci_dev *pdev, int num)
170 /* allocate attribute structure, piggyback attribute name */
171 int retval, nlen1, nlen2 = 0, res_count = 1;
172 unsigned long sparse_base, dense_base;
173 struct bin_attribute *attr;
174 struct pci_controller *hose = pdev->sysdata;
175 char *suffix, *attr_name;
177 suffix = ""; /* Assume bwx machine, normal resourceN files. */
180 if (pdev->resource[num].flags & IORESOURCE_MEM) {
181 sparse_base = hose->sparse_mem_base;
182 dense_base = hose->dense_mem_base;
183 if (sparse_base && !sparse_mem_mmap_fits(pdev, num)) {
186 nlen1 = 16; /* resourceN_dense */
189 sparse_base = hose->sparse_io_base;
190 dense_base = hose->dense_io_base;
197 nlen2 = 16; /* resourceN_dense */
202 attr = kzalloc(sizeof(*attr) * res_count + nlen1 + nlen2, GFP_ATOMIC);
206 /* Create bwx, sparse or single dense file */
207 attr_name = (char *)(attr + res_count);
208 pdev->res_attr[num] = attr;
209 retval = pci_create_one_attr(pdev, num, attr_name, suffix, attr,
211 if (retval || res_count == 1)
214 /* Create dense file */
217 pdev->res_attr_wc[num] = attr;
218 return pci_create_one_attr(pdev, num, attr_name, "_dense", attr, 0);
222 * pci_create_resource_files - create resource files in sysfs for @dev
223 * @dev: dev in question
225 * Walk the resources in @dev creating files for each resource available.
227 int pci_create_resource_files(struct pci_dev *pdev)
232 /* Expose the PCI resources from this device as files */
233 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
235 /* skip empty resources */
236 if (!pci_resource_len(pdev, i))
239 retval = pci_create_attr(pdev, i);
241 pci_remove_resource_files(pdev);
248 /* Legacy I/O bus mapping stuff. */
250 static int __legacy_mmap_fits(struct pci_controller *hose,
251 struct vm_area_struct *vma,
252 unsigned long res_size, int sparse)
254 unsigned long nr, start, size;
256 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
257 start = vma->vm_pgoff;
258 size = ((res_size - 1) >> PAGE_SHIFT) + 1;
260 if (start < size && size - start >= nr)
262 WARN(1, "process \"%s\" tried to map%s 0x%08lx-0x%08lx on hose %d "
264 current->comm, sparse ? " sparse" : "", start, start + nr,
269 static inline int has_sparse(struct pci_controller *hose,
270 enum pci_mmap_state mmap_type)
274 base = (mmap_type == pci_mmap_mem) ? hose->sparse_mem_base :
275 hose->sparse_io_base;
280 int pci_mmap_legacy_page_range(struct pci_bus *bus, struct vm_area_struct *vma,
281 enum pci_mmap_state mmap_type)
283 struct pci_controller *hose = bus->sysdata;
284 int sparse = has_sparse(hose, mmap_type);
285 unsigned long res_size;
287 res_size = (mmap_type == pci_mmap_mem) ? bus->legacy_mem->size :
288 bus->legacy_io->size;
289 if (!__legacy_mmap_fits(hose, vma, res_size, sparse))
292 return hose_mmap_page_range(hose, vma, mmap_type, sparse);
296 * pci_adjust_legacy_attr - adjustment of legacy file attributes
297 * @b: bus to create files under
298 * @mmap_type: I/O port or memory
300 * Adjust file name and size for sparse mappings.
302 void pci_adjust_legacy_attr(struct pci_bus *bus, enum pci_mmap_state mmap_type)
304 struct pci_controller *hose = bus->sysdata;
306 if (!has_sparse(hose, mmap_type))
309 if (mmap_type == pci_mmap_mem) {
310 bus->legacy_mem->attr.name = "legacy_mem_sparse";
311 bus->legacy_mem->size <<= 5;
313 bus->legacy_io->attr.name = "legacy_io_sparse";
314 bus->legacy_io->size <<= 5;
319 /* Legacy I/O bus read/write functions */
320 int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
322 struct pci_controller *hose = bus->sysdata;
324 port += hose->io_space->start;
328 *((u8 *)val) = inb(port);
333 *((u16 *)val) = inw(port);
338 *((u32 *)val) = inl(port);
344 int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
346 struct pci_controller *hose = bus->sysdata;
348 port += hose->io_space->start;