xref: /openbmc/linux/arch/alpha/kernel/pci-sysfs.c (revision 84fbfc33)
1 /*
2  * arch/alpha/kernel/pci-sysfs.c
3  *
4  * Copyright (C) 2009 Ivan Kokshaysky
5  *
6  * Alpha PCI resource files.
7  *
8  * Loosely based on generic HAVE_PCI_MMAP implementation in
9  * drivers/pci/pci-sysfs.c
10  */
11 
12 #include <linux/sched.h>
13 #include <linux/stat.h>
14 #include <linux/slab.h>
15 #include <linux/pci.h>
16 
17 static int hose_mmap_page_range(struct pci_controller *hose,
18 				struct vm_area_struct *vma,
19 				enum pci_mmap_state mmap_type, int sparse)
20 {
21 	unsigned long base;
22 
23 	if (mmap_type == pci_mmap_mem)
24 		base = sparse ? hose->sparse_mem_base : hose->dense_mem_base;
25 	else
26 		base = sparse ? hose->sparse_io_base : hose->dense_io_base;
27 
28 	vma->vm_pgoff += base >> PAGE_SHIFT;
29 
30 	return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
31 				  vma->vm_end - vma->vm_start,
32 				  vma->vm_page_prot);
33 }
34 
35 static int __pci_mmap_fits(struct pci_dev *pdev, int num,
36 			   struct vm_area_struct *vma, int sparse)
37 {
38 	unsigned long nr, start, size;
39 	int shift = sparse ? 5 : 0;
40 
41 	nr = vma_pages(vma);
42 	start = vma->vm_pgoff;
43 	size = ((pci_resource_len(pdev, num) - 1) >> (PAGE_SHIFT - shift)) + 1;
44 
45 	if (start < size && size - start >= nr)
46 		return 1;
47 	WARN(1, "process \"%s\" tried to map%s 0x%08lx-0x%08lx on %s BAR %d "
48 		"(size 0x%08lx)\n",
49 		current->comm, sparse ? " sparse" : "", start, start + nr,
50 		pci_name(pdev), num, size);
51 	return 0;
52 }
53 
54 /**
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
60  *
61  * Use the bus mapping routines to map a PCI resource into userspace.
62  */
63 static int pci_mmap_resource(struct kobject *kobj,
64 			     struct bin_attribute *attr,
65 			     struct vm_area_struct *vma, int sparse)
66 {
67 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
68 	struct resource *res = attr->private;
69 	enum pci_mmap_state mmap_type;
70 	struct pci_bus_region bar;
71 	int i;
72 
73 	for (i = 0; i < PCI_ROM_RESOURCE; i++)
74 		if (res == &pdev->resource[i])
75 			break;
76 	if (i >= PCI_ROM_RESOURCE)
77 		return -ENODEV;
78 
79 	if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
80 		return -EINVAL;
81 
82 	if (!__pci_mmap_fits(pdev, i, vma, sparse))
83 		return -EINVAL;
84 
85 	pcibios_resource_to_bus(pdev->bus, &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;
88 
89 	return hose_mmap_page_range(pdev->sysdata, vma, mmap_type, sparse);
90 }
91 
92 static int pci_mmap_resource_sparse(struct file *filp, struct kobject *kobj,
93 				    struct bin_attribute *attr,
94 				    struct vm_area_struct *vma)
95 {
96 	return pci_mmap_resource(kobj, attr, vma, 1);
97 }
98 
99 static int pci_mmap_resource_dense(struct file *filp, struct kobject *kobj,
100 				   struct bin_attribute *attr,
101 				   struct vm_area_struct *vma)
102 {
103 	return pci_mmap_resource(kobj, attr, vma, 0);
104 }
105 
106 /**
107  * pci_remove_resource_files - cleanup resource files
108  * @dev: dev to cleanup
109  *
110  * If we created resource files for @dev, remove them from sysfs and
111  * free their resources.
112  */
113 void pci_remove_resource_files(struct pci_dev *pdev)
114 {
115 	int i;
116 
117 	for (i = 0; i < PCI_ROM_RESOURCE; i++) {
118 		struct bin_attribute *res_attr;
119 
120 		res_attr = pdev->res_attr[i];
121 		if (res_attr) {
122 			sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
123 			kfree(res_attr);
124 		}
125 
126 		res_attr = pdev->res_attr_wc[i];
127 		if (res_attr) {
128 			sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
129 			kfree(res_attr);
130 		}
131 	}
132 }
133 
134 static int sparse_mem_mmap_fits(struct pci_dev *pdev, int num)
135 {
136 	struct pci_bus_region bar;
137 	struct pci_controller *hose = pdev->sysdata;
138 	long dense_offset;
139 	unsigned long sparse_size;
140 
141 	pcibios_resource_to_bus(pdev->bus, &bar, &pdev->resource[num]);
142 
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;
149 
150 	return bar.end < sparse_size;
151 }
152 
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)
156 {
157 	size_t size = pci_resource_len(pdev, num);
158 
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);
167 }
168 
169 static int pci_create_attr(struct pci_dev *pdev, int num)
170 {
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;
177 
178 	suffix = "";	/* Assume bwx machine, normal resourceN files. */
179 	nlen1 = 10;
180 
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)) {
185 			sparse_base = 0;
186 			suffix = "_dense";
187 			nlen1 = 16;	/* resourceN_dense */
188 		}
189 	} else {
190 		sparse_base = hose->sparse_io_base;
191 		dense_base = hose->dense_io_base;
192 	}
193 
194 	if (sparse_base) {
195 		suffix = "_sparse";
196 		nlen1 = 17;
197 		if (dense_base) {
198 			nlen2 = 16;	/* resourceN_dense */
199 			res_count = 2;
200 		}
201 	}
202 
203 	attr = kzalloc(sizeof(*attr) * res_count + nlen1 + nlen2, GFP_ATOMIC);
204 	if (!attr)
205 		return -ENOMEM;
206 
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,
211 				     sparse_base);
212 	if (retval || res_count == 1)
213 		return retval;
214 
215 	/* Create dense file */
216 	attr_name += nlen1;
217 	attr++;
218 	pdev->res_attr_wc[num] = attr;
219 	return pci_create_one_attr(pdev, num, attr_name, "_dense", attr, 0);
220 }
221 
222 /**
223  * pci_create_resource_files - create resource files in sysfs for @dev
224  * @dev: dev in question
225  *
226  * Walk the resources in @dev creating files for each resource available.
227  */
228 int pci_create_resource_files(struct pci_dev *pdev)
229 {
230 	int i;
231 	int retval;
232 
233 	/* Expose the PCI resources from this device as files */
234 	for (i = 0; i < PCI_ROM_RESOURCE; i++) {
235 
236 		/* skip empty resources */
237 		if (!pci_resource_len(pdev, i))
238 			continue;
239 
240 		retval = pci_create_attr(pdev, i);
241 		if (retval) {
242 			pci_remove_resource_files(pdev);
243 			return retval;
244 		}
245 	}
246 	return 0;
247 }
248 
249 /* Legacy I/O bus mapping stuff. */
250 
251 static int __legacy_mmap_fits(struct pci_controller *hose,
252 			      struct vm_area_struct *vma,
253 			      unsigned long res_size, int sparse)
254 {
255 	unsigned long nr, start, size;
256 
257 	nr = vma_pages(vma);
258 	start = vma->vm_pgoff;
259 	size = ((res_size - 1) >> PAGE_SHIFT) + 1;
260 
261 	if (start < size && size - start >= nr)
262 		return 1;
263 	WARN(1, "process \"%s\" tried to map%s 0x%08lx-0x%08lx on hose %d "
264 		"(size 0x%08lx)\n",
265 		current->comm, sparse ? " sparse" : "", start, start + nr,
266 		hose->index, size);
267 	return 0;
268 }
269 
270 static inline int has_sparse(struct pci_controller *hose,
271 			     enum pci_mmap_state mmap_type)
272 {
273 	unsigned long base;
274 
275 	base = (mmap_type == pci_mmap_mem) ? hose->sparse_mem_base :
276 					     hose->sparse_io_base;
277 
278 	return base != 0;
279 }
280 
281 int pci_mmap_legacy_page_range(struct pci_bus *bus, struct vm_area_struct *vma,
282 			       enum pci_mmap_state mmap_type)
283 {
284 	struct pci_controller *hose = bus->sysdata;
285 	int sparse = has_sparse(hose, mmap_type);
286 	unsigned long res_size;
287 
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))
291 		return -EINVAL;
292 
293 	return hose_mmap_page_range(hose, vma, mmap_type, sparse);
294 }
295 
296 /**
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
300  *
301  * Adjust file name and size for sparse mappings.
302  */
303 void pci_adjust_legacy_attr(struct pci_bus *bus, enum pci_mmap_state mmap_type)
304 {
305 	struct pci_controller *hose = bus->sysdata;
306 
307 	if (!has_sparse(hose, mmap_type))
308 		return;
309 
310 	if (mmap_type == pci_mmap_mem) {
311 		bus->legacy_mem->attr.name = "legacy_mem_sparse";
312 		bus->legacy_mem->size <<= 5;
313 	} else {
314 		bus->legacy_io->attr.name = "legacy_io_sparse";
315 		bus->legacy_io->size <<= 5;
316 	}
317 	return;
318 }
319 
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)
322 {
323 	struct pci_controller *hose = bus->sysdata;
324 
325 	port += hose->io_space->start;
326 
327 	switch(size) {
328 	case 1:
329 		*((u8 *)val) = inb(port);
330 		return 1;
331 	case 2:
332 		if (port & 1)
333 			return -EINVAL;
334 		*((u16 *)val) = inw(port);
335 		return 2;
336 	case 4:
337 		if (port & 3)
338 			return -EINVAL;
339 		*((u32 *)val) = inl(port);
340 		return 4;
341 	}
342 	return -EINVAL;
343 }
344 
345 int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
346 {
347 	struct pci_controller *hose = bus->sysdata;
348 
349 	port += hose->io_space->start;
350 
351 	switch(size) {
352 	case 1:
353 		outb(port, val);
354 		return 1;
355 	case 2:
356 		if (port & 1)
357 			return -EINVAL;
358 		outw(port, val);
359 		return 2;
360 	case 4:
361 		if (port & 3)
362 			return -EINVAL;
363 		outl(port, val);
364 		return 4;
365 	}
366 	return -EINVAL;
367 }
368