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
,
64 struct bin_attribute
*attr
,
65 struct vm_area_struct
*vma
, int sparse
)
67 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
,
68 struct device
, kobj
));
69 struct resource
*res
= attr
->private;
70 enum pci_mmap_state mmap_type
;
71 struct pci_bus_region bar
;
74 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++)
75 if (res
== &pdev
->resource
[i
])
77 if (i
>= PCI_ROM_RESOURCE
)
80 if (!__pci_mmap_fits(pdev
, i
, vma
, sparse
))
83 if (iomem_is_exclusive(res
->start
))
86 pcibios_resource_to_bus(pdev
, &bar
, res
);
87 vma
->vm_pgoff
+= bar
.start
>> (PAGE_SHIFT
- (sparse
? 5 : 0));
88 mmap_type
= res
->flags
& IORESOURCE_MEM
? pci_mmap_mem
: pci_mmap_io
;
90 return hose_mmap_page_range(pdev
->sysdata
, vma
, mmap_type
, sparse
);
93 static int pci_mmap_resource_sparse(struct file
*filp
, struct kobject
*kobj
,
94 struct bin_attribute
*attr
,
95 struct vm_area_struct
*vma
)
97 return pci_mmap_resource(kobj
, attr
, vma
, 1);
100 static int pci_mmap_resource_dense(struct file
*filp
, struct kobject
*kobj
,
101 struct bin_attribute
*attr
,
102 struct vm_area_struct
*vma
)
104 return pci_mmap_resource(kobj
, attr
, vma
, 0);
108 * pci_remove_resource_files - cleanup resource files
109 * @dev: dev to cleanup
111 * If we created resource files for @dev, remove them from sysfs and
112 * free their resources.
114 void pci_remove_resource_files(struct pci_dev
*pdev
)
118 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
119 struct bin_attribute
*res_attr
;
121 res_attr
= pdev
->res_attr
[i
];
123 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
127 res_attr
= pdev
->res_attr_wc
[i
];
129 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
135 static int sparse_mem_mmap_fits(struct pci_dev
*pdev
, int num
)
137 struct pci_bus_region bar
;
138 struct pci_controller
*hose
= pdev
->sysdata
;
140 unsigned long sparse_size
;
142 pcibios_resource_to_bus(pdev
, &bar
, &pdev
->resource
[num
]);
144 /* All core logic chips have 4G sparse address space, except
145 CIA which has 16G (see xxx_SPARSE_MEM and xxx_DENSE_MEM
146 definitions in asm/core_xxx.h files). This corresponds
147 to 128M or 512M of the bus space. */
148 dense_offset
= (long)(hose
->dense_mem_base
- hose
->sparse_mem_base
);
149 sparse_size
= dense_offset
>= 0x400000000UL
? 0x20000000 : 0x8000000;
151 return bar
.end
< sparse_size
;
154 static int pci_create_one_attr(struct pci_dev
*pdev
, int num
, char *name
,
155 char *suffix
, struct bin_attribute
*res_attr
,
156 unsigned long sparse
)
158 size_t size
= pci_resource_len(pdev
, num
);
160 sprintf(name
, "resource%d%s", num
, suffix
);
161 res_attr
->mmap
= sparse
? pci_mmap_resource_sparse
:
162 pci_mmap_resource_dense
;
163 res_attr
->attr
.name
= name
;
164 res_attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
165 res_attr
->size
= sparse
? size
<< 5 : size
;
166 res_attr
->private = &pdev
->resource
[num
];
167 return sysfs_create_bin_file(&pdev
->dev
.kobj
, res_attr
);
170 static int pci_create_attr(struct pci_dev
*pdev
, int num
)
172 /* allocate attribute structure, piggyback attribute name */
173 int retval
, nlen1
, nlen2
= 0, res_count
= 1;
174 unsigned long sparse_base
, dense_base
;
175 struct bin_attribute
*attr
;
176 struct pci_controller
*hose
= pdev
->sysdata
;
177 char *suffix
, *attr_name
;
179 suffix
= ""; /* Assume bwx machine, normal resourceN files. */
182 if (pdev
->resource
[num
].flags
& IORESOURCE_MEM
) {
183 sparse_base
= hose
->sparse_mem_base
;
184 dense_base
= hose
->dense_mem_base
;
185 if (sparse_base
&& !sparse_mem_mmap_fits(pdev
, num
)) {
188 nlen1
= 16; /* resourceN_dense */
191 sparse_base
= hose
->sparse_io_base
;
192 dense_base
= hose
->dense_io_base
;
199 nlen2
= 16; /* resourceN_dense */
204 attr
= kzalloc(sizeof(*attr
) * res_count
+ nlen1
+ nlen2
, GFP_ATOMIC
);
208 /* Create bwx, sparse or single dense file */
209 attr_name
= (char *)(attr
+ res_count
);
210 pdev
->res_attr
[num
] = attr
;
211 retval
= pci_create_one_attr(pdev
, num
, attr_name
, suffix
, attr
,
213 if (retval
|| res_count
== 1)
216 /* Create dense file */
219 pdev
->res_attr_wc
[num
] = attr
;
220 return pci_create_one_attr(pdev
, num
, attr_name
, "_dense", attr
, 0);
224 * pci_create_resource_files - create resource files in sysfs for @dev
225 * @dev: dev in question
227 * Walk the resources in @dev creating files for each resource available.
229 int pci_create_resource_files(struct pci_dev
*pdev
)
234 /* Expose the PCI resources from this device as files */
235 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
237 /* skip empty resources */
238 if (!pci_resource_len(pdev
, i
))
241 retval
= pci_create_attr(pdev
, i
);
243 pci_remove_resource_files(pdev
);
250 /* Legacy I/O bus mapping stuff. */
252 static int __legacy_mmap_fits(struct pci_controller
*hose
,
253 struct vm_area_struct
*vma
,
254 unsigned long res_size
, int sparse
)
256 unsigned long nr
, start
, size
;
258 nr
= (vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
;
259 start
= vma
->vm_pgoff
;
260 size
= ((res_size
- 1) >> PAGE_SHIFT
) + 1;
262 if (start
< size
&& size
- start
>= nr
)
264 WARN(1, "process \"%s\" tried to map%s 0x%08lx-0x%08lx on hose %d "
266 current
->comm
, sparse
? " sparse" : "", start
, start
+ nr
,
271 static inline int has_sparse(struct pci_controller
*hose
,
272 enum pci_mmap_state mmap_type
)
276 base
= (mmap_type
== pci_mmap_mem
) ? hose
->sparse_mem_base
:
277 hose
->sparse_io_base
;
282 int pci_mmap_legacy_page_range(struct pci_bus
*bus
, struct vm_area_struct
*vma
,
283 enum pci_mmap_state mmap_type
)
285 struct pci_controller
*hose
= bus
->sysdata
;
286 int sparse
= has_sparse(hose
, mmap_type
);
287 unsigned long res_size
;
289 res_size
= (mmap_type
== pci_mmap_mem
) ? bus
->legacy_mem
->size
:
290 bus
->legacy_io
->size
;
291 if (!__legacy_mmap_fits(hose
, vma
, res_size
, sparse
))
294 return hose_mmap_page_range(hose
, vma
, mmap_type
, sparse
);
298 * pci_adjust_legacy_attr - adjustment of legacy file attributes
299 * @b: bus to create files under
300 * @mmap_type: I/O port or memory
302 * Adjust file name and size for sparse mappings.
304 void pci_adjust_legacy_attr(struct pci_bus
*bus
, enum pci_mmap_state mmap_type
)
306 struct pci_controller
*hose
= bus
->sysdata
;
308 if (!has_sparse(hose
, mmap_type
))
311 if (mmap_type
== pci_mmap_mem
) {
312 bus
->legacy_mem
->attr
.name
= "legacy_mem_sparse";
313 bus
->legacy_mem
->size
<<= 5;
315 bus
->legacy_io
->attr
.name
= "legacy_io_sparse";
316 bus
->legacy_io
->size
<<= 5;
321 /* Legacy I/O bus read/write functions */
322 int pci_legacy_read(struct pci_bus
*bus
, loff_t port
, u32
*val
, size_t size
)
324 struct pci_controller
*hose
= bus
->sysdata
;
326 port
+= hose
->io_space
->start
;
330 *((u8
*)val
) = inb(port
);
335 *((u16
*)val
) = inw(port
);
340 *((u32
*)val
) = inl(port
);
346 int pci_legacy_write(struct pci_bus
*bus
, loff_t port
, u32 val
, size_t size
)
348 struct pci_controller
*hose
= bus
->sysdata
;
350 port
+= hose
->io_space
->start
;