treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / arch / alpha / kernel / pci-sysfs.c
blob0021580d79adbd577e5829841323ec1e889a1e30
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * arch/alpha/kernel/pci-sysfs.c
5 * Copyright (C) 2009 Ivan Kokshaysky
7 * Alpha PCI resource files.
9 * Loosely based on generic HAVE_PCI_MMAP implementation in
10 * drivers/pci/pci-sysfs.c
13 #include <linux/sched.h>
14 #include <linux/stat.h>
15 #include <linux/slab.h>
16 #include <linux/pci.h>
18 static int hose_mmap_page_range(struct pci_controller *hose,
19 struct vm_area_struct *vma,
20 enum pci_mmap_state mmap_type, int sparse)
22 unsigned long base;
24 if (mmap_type == pci_mmap_mem)
25 base = sparse ? hose->sparse_mem_base : hose->dense_mem_base;
26 else
27 base = sparse ? hose->sparse_io_base : hose->dense_io_base;
29 vma->vm_pgoff += base >> PAGE_SHIFT;
31 return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
32 vma->vm_end - vma->vm_start,
33 vma->vm_page_prot);
36 static int __pci_mmap_fits(struct pci_dev *pdev, int num,
37 struct vm_area_struct *vma, int sparse)
39 unsigned long nr, start, size;
40 int shift = sparse ? 5 : 0;
42 nr = vma_pages(vma);
43 start = vma->vm_pgoff;
44 size = ((pci_resource_len(pdev, num) - 1) >> (PAGE_SHIFT - shift)) + 1;
46 if (start < size && size - start >= nr)
47 return 1;
48 WARN(1, "process \"%s\" tried to map%s 0x%08lx-0x%08lx on %s BAR %d "
49 "(size 0x%08lx)\n",
50 current->comm, sparse ? " sparse" : "", start, start + nr,
51 pci_name(pdev), num, size);
52 return 0;
55 /**
56 * pci_mmap_resource - map a PCI resource into user memory space
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 kobject *kobj,
65 struct bin_attribute *attr,
66 struct vm_area_struct *vma, int sparse)
68 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
69 struct resource *res = attr->private;
70 enum pci_mmap_state mmap_type;
71 struct pci_bus_region bar;
72 int i;
74 for (i = 0; i < PCI_STD_NUM_BARS; i++)
75 if (res == &pdev->resource[i])
76 break;
77 if (i >= PCI_STD_NUM_BARS)
78 return -ENODEV;
80 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
81 return -EINVAL;
83 if (!__pci_mmap_fits(pdev, i, vma, sparse))
84 return -EINVAL;
86 pcibios_resource_to_bus(pdev->bus, &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)
116 int i;
118 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
119 struct bin_attribute *res_attr;
121 res_attr = pdev->res_attr[i];
122 if (res_attr) {
123 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
124 kfree(res_attr);
127 res_attr = pdev->res_attr_wc[i];
128 if (res_attr) {
129 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
130 kfree(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;
139 long dense_offset;
140 unsigned long sparse_size;
142 pcibios_resource_to_bus(pdev->bus, &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. */
180 nlen1 = 10;
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)) {
186 sparse_base = 0;
187 suffix = "_dense";
188 nlen1 = 16; /* resourceN_dense */
190 } else {
191 sparse_base = hose->sparse_io_base;
192 dense_base = hose->dense_io_base;
195 if (sparse_base) {
196 suffix = "_sparse";
197 nlen1 = 17;
198 if (dense_base) {
199 nlen2 = 16; /* resourceN_dense */
200 res_count = 2;
204 attr = kzalloc(sizeof(*attr) * res_count + nlen1 + nlen2, GFP_ATOMIC);
205 if (!attr)
206 return -ENOMEM;
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,
212 sparse_base);
213 if (retval || res_count == 1)
214 return retval;
216 /* Create dense file */
217 attr_name += nlen1;
218 attr++;
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)
231 int i;
232 int retval;
234 /* Expose the PCI resources from this device as files */
235 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
237 /* skip empty resources */
238 if (!pci_resource_len(pdev, i))
239 continue;
241 retval = pci_create_attr(pdev, i);
242 if (retval) {
243 pci_remove_resource_files(pdev);
244 return retval;
247 return 0;
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_pages(vma);
259 start = vma->vm_pgoff;
260 size = ((res_size - 1) >> PAGE_SHIFT) + 1;
262 if (start < size && size - start >= nr)
263 return 1;
264 WARN(1, "process \"%s\" tried to map%s 0x%08lx-0x%08lx on hose %d "
265 "(size 0x%08lx)\n",
266 current->comm, sparse ? " sparse" : "", start, start + nr,
267 hose->index, size);
268 return 0;
271 static inline int has_sparse(struct pci_controller *hose,
272 enum pci_mmap_state mmap_type)
274 unsigned long base;
276 base = (mmap_type == pci_mmap_mem) ? hose->sparse_mem_base :
277 hose->sparse_io_base;
279 return base != 0;
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))
292 return -EINVAL;
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))
309 return;
311 if (mmap_type == pci_mmap_mem) {
312 bus->legacy_mem->attr.name = "legacy_mem_sparse";
313 bus->legacy_mem->size <<= 5;
314 } else {
315 bus->legacy_io->attr.name = "legacy_io_sparse";
316 bus->legacy_io->size <<= 5;
318 return;
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;
328 switch(size) {
329 case 1:
330 *((u8 *)val) = inb(port);
331 return 1;
332 case 2:
333 if (port & 1)
334 return -EINVAL;
335 *((u16 *)val) = inw(port);
336 return 2;
337 case 4:
338 if (port & 3)
339 return -EINVAL;
340 *((u32 *)val) = inl(port);
341 return 4;
343 return -EINVAL;
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;
352 switch(size) {
353 case 1:
354 outb(port, val);
355 return 1;
356 case 2:
357 if (port & 1)
358 return -EINVAL;
359 outw(port, val);
360 return 2;
361 case 4:
362 if (port & 3)
363 return -EINVAL;
364 outl(port, val);
365 return 4;
367 return -EINVAL;