Loading...
1// SPDX-License-Identifier: GPL-2.0
2/* pci_common.c: PCI controller common support.
3 *
4 * Copyright (C) 1999, 2007 David S. Miller (davem@davemloft.net)
5 */
6
7#include <linux/string.h>
8#include <linux/slab.h>
9#include <linux/pci.h>
10#include <linux/device.h>
11#include <linux/of_device.h>
12
13#include <asm/prom.h>
14#include <asm/oplib.h>
15
16#include "pci_impl.h"
17#include "pci_sun4v.h"
18
19static int config_out_of_range(struct pci_pbm_info *pbm,
20 unsigned long bus,
21 unsigned long devfn,
22 unsigned long reg)
23{
24 if (bus < pbm->pci_first_busno ||
25 bus > pbm->pci_last_busno)
26 return 1;
27 return 0;
28}
29
30static void *sun4u_config_mkaddr(struct pci_pbm_info *pbm,
31 unsigned long bus,
32 unsigned long devfn,
33 unsigned long reg)
34{
35 unsigned long rbits = pbm->config_space_reg_bits;
36
37 if (config_out_of_range(pbm, bus, devfn, reg))
38 return NULL;
39
40 reg = (reg & ((1 << rbits) - 1));
41 devfn <<= rbits;
42 bus <<= rbits + 8;
43
44 return (void *) (pbm->config_space | bus | devfn | reg);
45}
46
47/* At least on Sabre, it is necessary to access all PCI host controller
48 * registers at their natural size, otherwise zeros are returned.
49 * Strange but true, and I see no language in the UltraSPARC-IIi
50 * programmer's manual that mentions this even indirectly.
51 */
52static int sun4u_read_pci_cfg_host(struct pci_pbm_info *pbm,
53 unsigned char bus, unsigned int devfn,
54 int where, int size, u32 *value)
55{
56 u32 tmp32, *addr;
57 u16 tmp16;
58 u8 tmp8;
59
60 addr = sun4u_config_mkaddr(pbm, bus, devfn, where);
61 if (!addr)
62 return PCIBIOS_SUCCESSFUL;
63
64 switch (size) {
65 case 1:
66 if (where < 8) {
67 unsigned long align = (unsigned long) addr;
68
69 align &= ~1;
70 pci_config_read16((u16 *)align, &tmp16);
71 if (where & 1)
72 *value = tmp16 >> 8;
73 else
74 *value = tmp16 & 0xff;
75 } else {
76 pci_config_read8((u8 *)addr, &tmp8);
77 *value = (u32) tmp8;
78 }
79 break;
80
81 case 2:
82 if (where < 8) {
83 pci_config_read16((u16 *)addr, &tmp16);
84 *value = (u32) tmp16;
85 } else {
86 pci_config_read8((u8 *)addr, &tmp8);
87 *value = (u32) tmp8;
88 pci_config_read8(((u8 *)addr) + 1, &tmp8);
89 *value |= ((u32) tmp8) << 8;
90 }
91 break;
92
93 case 4:
94 tmp32 = 0xffffffff;
95 sun4u_read_pci_cfg_host(pbm, bus, devfn,
96 where, 2, &tmp32);
97 *value = tmp32;
98
99 tmp32 = 0xffffffff;
100 sun4u_read_pci_cfg_host(pbm, bus, devfn,
101 where + 2, 2, &tmp32);
102 *value |= tmp32 << 16;
103 break;
104 }
105 return PCIBIOS_SUCCESSFUL;
106}
107
108static int sun4u_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
109 int where, int size, u32 *value)
110{
111 struct pci_pbm_info *pbm = bus_dev->sysdata;
112 unsigned char bus = bus_dev->number;
113 u32 *addr;
114 u16 tmp16;
115 u8 tmp8;
116
117 switch (size) {
118 case 1:
119 *value = 0xff;
120 break;
121 case 2:
122 *value = 0xffff;
123 break;
124 case 4:
125 *value = 0xffffffff;
126 break;
127 }
128
129 if (!bus_dev->number && !PCI_SLOT(devfn))
130 return sun4u_read_pci_cfg_host(pbm, bus, devfn, where,
131 size, value);
132
133 addr = sun4u_config_mkaddr(pbm, bus, devfn, where);
134 if (!addr)
135 return PCIBIOS_SUCCESSFUL;
136
137 switch (size) {
138 case 1:
139 pci_config_read8((u8 *)addr, &tmp8);
140 *value = (u32) tmp8;
141 break;
142
143 case 2:
144 if (where & 0x01) {
145 printk("pci_read_config_word: misaligned reg [%x]\n",
146 where);
147 return PCIBIOS_SUCCESSFUL;
148 }
149 pci_config_read16((u16 *)addr, &tmp16);
150 *value = (u32) tmp16;
151 break;
152
153 case 4:
154 if (where & 0x03) {
155 printk("pci_read_config_dword: misaligned reg [%x]\n",
156 where);
157 return PCIBIOS_SUCCESSFUL;
158 }
159 pci_config_read32(addr, value);
160 break;
161 }
162 return PCIBIOS_SUCCESSFUL;
163}
164
165static int sun4u_write_pci_cfg_host(struct pci_pbm_info *pbm,
166 unsigned char bus, unsigned int devfn,
167 int where, int size, u32 value)
168{
169 u32 *addr;
170
171 addr = sun4u_config_mkaddr(pbm, bus, devfn, where);
172 if (!addr)
173 return PCIBIOS_SUCCESSFUL;
174
175 switch (size) {
176 case 1:
177 if (where < 8) {
178 unsigned long align = (unsigned long) addr;
179 u16 tmp16;
180
181 align &= ~1;
182 pci_config_read16((u16 *)align, &tmp16);
183 if (where & 1) {
184 tmp16 &= 0x00ff;
185 tmp16 |= value << 8;
186 } else {
187 tmp16 &= 0xff00;
188 tmp16 |= value;
189 }
190 pci_config_write16((u16 *)align, tmp16);
191 } else
192 pci_config_write8((u8 *)addr, value);
193 break;
194 case 2:
195 if (where < 8) {
196 pci_config_write16((u16 *)addr, value);
197 } else {
198 pci_config_write8((u8 *)addr, value & 0xff);
199 pci_config_write8(((u8 *)addr) + 1, value >> 8);
200 }
201 break;
202 case 4:
203 sun4u_write_pci_cfg_host(pbm, bus, devfn,
204 where, 2, value & 0xffff);
205 sun4u_write_pci_cfg_host(pbm, bus, devfn,
206 where + 2, 2, value >> 16);
207 break;
208 }
209 return PCIBIOS_SUCCESSFUL;
210}
211
212static int sun4u_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
213 int where, int size, u32 value)
214{
215 struct pci_pbm_info *pbm = bus_dev->sysdata;
216 unsigned char bus = bus_dev->number;
217 u32 *addr;
218
219 if (!bus_dev->number && !PCI_SLOT(devfn))
220 return sun4u_write_pci_cfg_host(pbm, bus, devfn, where,
221 size, value);
222
223 addr = sun4u_config_mkaddr(pbm, bus, devfn, where);
224 if (!addr)
225 return PCIBIOS_SUCCESSFUL;
226
227 switch (size) {
228 case 1:
229 pci_config_write8((u8 *)addr, value);
230 break;
231
232 case 2:
233 if (where & 0x01) {
234 printk("pci_write_config_word: misaligned reg [%x]\n",
235 where);
236 return PCIBIOS_SUCCESSFUL;
237 }
238 pci_config_write16((u16 *)addr, value);
239 break;
240
241 case 4:
242 if (where & 0x03) {
243 printk("pci_write_config_dword: misaligned reg [%x]\n",
244 where);
245 return PCIBIOS_SUCCESSFUL;
246 }
247 pci_config_write32(addr, value);
248 }
249 return PCIBIOS_SUCCESSFUL;
250}
251
252struct pci_ops sun4u_pci_ops = {
253 .read = sun4u_read_pci_cfg,
254 .write = sun4u_write_pci_cfg,
255};
256
257static int sun4v_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
258 int where, int size, u32 *value)
259{
260 struct pci_pbm_info *pbm = bus_dev->sysdata;
261 u32 devhandle = pbm->devhandle;
262 unsigned int bus = bus_dev->number;
263 unsigned int device = PCI_SLOT(devfn);
264 unsigned int func = PCI_FUNC(devfn);
265 unsigned long ret;
266
267 if (config_out_of_range(pbm, bus, devfn, where)) {
268 ret = ~0UL;
269 } else {
270 ret = pci_sun4v_config_get(devhandle,
271 HV_PCI_DEVICE_BUILD(bus, device, func),
272 where, size);
273 }
274 switch (size) {
275 case 1:
276 *value = ret & 0xff;
277 break;
278 case 2:
279 *value = ret & 0xffff;
280 break;
281 case 4:
282 *value = ret & 0xffffffff;
283 break;
284 }
285
286
287 return PCIBIOS_SUCCESSFUL;
288}
289
290static int sun4v_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
291 int where, int size, u32 value)
292{
293 struct pci_pbm_info *pbm = bus_dev->sysdata;
294 u32 devhandle = pbm->devhandle;
295 unsigned int bus = bus_dev->number;
296 unsigned int device = PCI_SLOT(devfn);
297 unsigned int func = PCI_FUNC(devfn);
298
299 if (config_out_of_range(pbm, bus, devfn, where)) {
300 /* Do nothing. */
301 } else {
302 /* We don't check for hypervisor errors here, but perhaps
303 * we should and influence our return value depending upon
304 * what kind of error is thrown.
305 */
306 pci_sun4v_config_put(devhandle,
307 HV_PCI_DEVICE_BUILD(bus, device, func),
308 where, size, value);
309 }
310 return PCIBIOS_SUCCESSFUL;
311}
312
313struct pci_ops sun4v_pci_ops = {
314 .read = sun4v_read_pci_cfg,
315 .write = sun4v_write_pci_cfg,
316};
317
318void pci_get_pbm_props(struct pci_pbm_info *pbm)
319{
320 const u32 *val = of_get_property(pbm->op->dev.of_node, "bus-range", NULL);
321
322 pbm->pci_first_busno = val[0];
323 pbm->pci_last_busno = val[1];
324
325 val = of_get_property(pbm->op->dev.of_node, "ino-bitmap", NULL);
326 if (val) {
327 pbm->ino_bitmap = (((u64)val[1] << 32UL) |
328 ((u64)val[0] << 0UL));
329 }
330}
331
332static void pci_register_legacy_regions(struct resource *io_res,
333 struct resource *mem_res)
334{
335 struct resource *p;
336
337 /* VGA Video RAM. */
338 p = kzalloc(sizeof(*p), GFP_KERNEL);
339 if (!p)
340 return;
341
342 p->name = "Video RAM area";
343 p->start = mem_res->start + 0xa0000UL;
344 p->end = p->start + 0x1ffffUL;
345 p->flags = IORESOURCE_BUSY;
346 request_resource(mem_res, p);
347}
348
349static void pci_register_iommu_region(struct pci_pbm_info *pbm)
350{
351 const u32 *vdma = of_get_property(pbm->op->dev.of_node, "virtual-dma",
352 NULL);
353
354 if (vdma) {
355 struct resource *rp = kzalloc(sizeof(*rp), GFP_KERNEL);
356
357 if (!rp) {
358 pr_info("%s: Cannot allocate IOMMU resource.\n",
359 pbm->name);
360 return;
361 }
362 rp->name = "IOMMU";
363 rp->start = pbm->mem_space.start + (unsigned long) vdma[0];
364 rp->end = rp->start + (unsigned long) vdma[1] - 1UL;
365 rp->flags = IORESOURCE_BUSY;
366 if (request_resource(&pbm->mem_space, rp)) {
367 pr_info("%s: Unable to request IOMMU resource.\n",
368 pbm->name);
369 kfree(rp);
370 }
371 }
372}
373
374void pci_determine_mem_io_space(struct pci_pbm_info *pbm)
375{
376 const struct linux_prom_pci_ranges *pbm_ranges;
377 int i, saw_mem, saw_io;
378 int num_pbm_ranges;
379
380 /* Corresponding generic code in of_pci_get_host_bridge_resources() */
381
382 saw_mem = saw_io = 0;
383 pbm_ranges = of_get_property(pbm->op->dev.of_node, "ranges", &i);
384 if (!pbm_ranges) {
385 prom_printf("PCI: Fatal error, missing PBM ranges property "
386 " for %s\n",
387 pbm->name);
388 prom_halt();
389 }
390
391 num_pbm_ranges = i / sizeof(*pbm_ranges);
392 memset(&pbm->mem64_space, 0, sizeof(struct resource));
393
394 for (i = 0; i < num_pbm_ranges; i++) {
395 const struct linux_prom_pci_ranges *pr = &pbm_ranges[i];
396 unsigned long a, size, region_a;
397 u32 parent_phys_hi, parent_phys_lo;
398 u32 child_phys_mid, child_phys_lo;
399 u32 size_hi, size_lo;
400 int type;
401
402 parent_phys_hi = pr->parent_phys_hi;
403 parent_phys_lo = pr->parent_phys_lo;
404 child_phys_mid = pr->child_phys_mid;
405 child_phys_lo = pr->child_phys_lo;
406 if (tlb_type == hypervisor)
407 parent_phys_hi &= 0x0fffffff;
408
409 size_hi = pr->size_hi;
410 size_lo = pr->size_lo;
411
412 type = (pr->child_phys_hi >> 24) & 0x3;
413 a = (((unsigned long)parent_phys_hi << 32UL) |
414 ((unsigned long)parent_phys_lo << 0UL));
415 region_a = (((unsigned long)child_phys_mid << 32UL) |
416 ((unsigned long)child_phys_lo << 0UL));
417 size = (((unsigned long)size_hi << 32UL) |
418 ((unsigned long)size_lo << 0UL));
419
420 switch (type) {
421 case 0:
422 /* PCI config space, 16MB */
423 pbm->config_space = a;
424 break;
425
426 case 1:
427 /* 16-bit IO space, 16MB */
428 pbm->io_space.start = a;
429 pbm->io_space.end = a + size - 1UL;
430 pbm->io_space.flags = IORESOURCE_IO;
431 pbm->io_offset = a - region_a;
432 saw_io = 1;
433 break;
434
435 case 2:
436 /* 32-bit MEM space, 2GB */
437 pbm->mem_space.start = a;
438 pbm->mem_space.end = a + size - 1UL;
439 pbm->mem_space.flags = IORESOURCE_MEM;
440 pbm->mem_offset = a - region_a;
441 saw_mem = 1;
442 break;
443
444 case 3:
445 /* 64-bit MEM handling */
446 pbm->mem64_space.start = a;
447 pbm->mem64_space.end = a + size - 1UL;
448 pbm->mem64_space.flags = IORESOURCE_MEM;
449 pbm->mem64_offset = a - region_a;
450 saw_mem = 1;
451 break;
452
453 default:
454 break;
455 }
456 }
457
458 if (!saw_io || !saw_mem) {
459 prom_printf("%s: Fatal error, missing %s PBM range.\n",
460 pbm->name,
461 (!saw_io ? "IO" : "MEM"));
462 prom_halt();
463 }
464
465 if (pbm->io_space.flags)
466 printk("%s: PCI IO %pR offset %llx\n",
467 pbm->name, &pbm->io_space, pbm->io_offset);
468 if (pbm->mem_space.flags)
469 printk("%s: PCI MEM %pR offset %llx\n",
470 pbm->name, &pbm->mem_space, pbm->mem_offset);
471 if (pbm->mem64_space.flags && pbm->mem_space.flags) {
472 if (pbm->mem64_space.start <= pbm->mem_space.end)
473 pbm->mem64_space.start = pbm->mem_space.end + 1;
474 if (pbm->mem64_space.start > pbm->mem64_space.end)
475 pbm->mem64_space.flags = 0;
476 }
477
478 if (pbm->mem64_space.flags)
479 printk("%s: PCI MEM64 %pR offset %llx\n",
480 pbm->name, &pbm->mem64_space, pbm->mem64_offset);
481
482 pbm->io_space.name = pbm->mem_space.name = pbm->name;
483 pbm->mem64_space.name = pbm->name;
484
485 request_resource(&ioport_resource, &pbm->io_space);
486 request_resource(&iomem_resource, &pbm->mem_space);
487 if (pbm->mem64_space.flags)
488 request_resource(&iomem_resource, &pbm->mem64_space);
489
490 pci_register_legacy_regions(&pbm->io_space,
491 &pbm->mem_space);
492 pci_register_iommu_region(pbm);
493}
494
495/* Generic helper routines for PCI error reporting. */
496void pci_scan_for_target_abort(struct pci_pbm_info *pbm,
497 struct pci_bus *pbus)
498{
499 struct pci_dev *pdev;
500 struct pci_bus *bus;
501
502 list_for_each_entry(pdev, &pbus->devices, bus_list) {
503 u16 status, error_bits;
504
505 pci_read_config_word(pdev, PCI_STATUS, &status);
506 error_bits =
507 (status & (PCI_STATUS_SIG_TARGET_ABORT |
508 PCI_STATUS_REC_TARGET_ABORT));
509 if (error_bits) {
510 pci_write_config_word(pdev, PCI_STATUS, error_bits);
511 printk("%s: Device %s saw Target Abort [%016x]\n",
512 pbm->name, pci_name(pdev), status);
513 }
514 }
515
516 list_for_each_entry(bus, &pbus->children, node)
517 pci_scan_for_target_abort(pbm, bus);
518}
519
520void pci_scan_for_master_abort(struct pci_pbm_info *pbm,
521 struct pci_bus *pbus)
522{
523 struct pci_dev *pdev;
524 struct pci_bus *bus;
525
526 list_for_each_entry(pdev, &pbus->devices, bus_list) {
527 u16 status, error_bits;
528
529 pci_read_config_word(pdev, PCI_STATUS, &status);
530 error_bits =
531 (status & (PCI_STATUS_REC_MASTER_ABORT));
532 if (error_bits) {
533 pci_write_config_word(pdev, PCI_STATUS, error_bits);
534 printk("%s: Device %s received Master Abort [%016x]\n",
535 pbm->name, pci_name(pdev), status);
536 }
537 }
538
539 list_for_each_entry(bus, &pbus->children, node)
540 pci_scan_for_master_abort(pbm, bus);
541}
542
543void pci_scan_for_parity_error(struct pci_pbm_info *pbm,
544 struct pci_bus *pbus)
545{
546 struct pci_dev *pdev;
547 struct pci_bus *bus;
548
549 list_for_each_entry(pdev, &pbus->devices, bus_list) {
550 u16 status, error_bits;
551
552 pci_read_config_word(pdev, PCI_STATUS, &status);
553 error_bits =
554 (status & (PCI_STATUS_PARITY |
555 PCI_STATUS_DETECTED_PARITY));
556 if (error_bits) {
557 pci_write_config_word(pdev, PCI_STATUS, error_bits);
558 printk("%s: Device %s saw Parity Error [%016x]\n",
559 pbm->name, pci_name(pdev), status);
560 }
561 }
562
563 list_for_each_entry(bus, &pbus->children, node)
564 pci_scan_for_parity_error(pbm, bus);
565}
1/* pci_common.c: PCI controller common support.
2 *
3 * Copyright (C) 1999, 2007 David S. Miller (davem@davemloft.net)
4 */
5
6#include <linux/string.h>
7#include <linux/slab.h>
8#include <linux/pci.h>
9#include <linux/device.h>
10#include <linux/of_device.h>
11
12#include <asm/prom.h>
13#include <asm/oplib.h>
14
15#include "pci_impl.h"
16#include "pci_sun4v.h"
17
18static int config_out_of_range(struct pci_pbm_info *pbm,
19 unsigned long bus,
20 unsigned long devfn,
21 unsigned long reg)
22{
23 if (bus < pbm->pci_first_busno ||
24 bus > pbm->pci_last_busno)
25 return 1;
26 return 0;
27}
28
29static void *sun4u_config_mkaddr(struct pci_pbm_info *pbm,
30 unsigned long bus,
31 unsigned long devfn,
32 unsigned long reg)
33{
34 unsigned long rbits = pbm->config_space_reg_bits;
35
36 if (config_out_of_range(pbm, bus, devfn, reg))
37 return NULL;
38
39 reg = (reg & ((1 << rbits) - 1));
40 devfn <<= rbits;
41 bus <<= rbits + 8;
42
43 return (void *) (pbm->config_space | bus | devfn | reg);
44}
45
46/* At least on Sabre, it is necessary to access all PCI host controller
47 * registers at their natural size, otherwise zeros are returned.
48 * Strange but true, and I see no language in the UltraSPARC-IIi
49 * programmer's manual that mentions this even indirectly.
50 */
51static int sun4u_read_pci_cfg_host(struct pci_pbm_info *pbm,
52 unsigned char bus, unsigned int devfn,
53 int where, int size, u32 *value)
54{
55 u32 tmp32, *addr;
56 u16 tmp16;
57 u8 tmp8;
58
59 addr = sun4u_config_mkaddr(pbm, bus, devfn, where);
60 if (!addr)
61 return PCIBIOS_SUCCESSFUL;
62
63 switch (size) {
64 case 1:
65 if (where < 8) {
66 unsigned long align = (unsigned long) addr;
67
68 align &= ~1;
69 pci_config_read16((u16 *)align, &tmp16);
70 if (where & 1)
71 *value = tmp16 >> 8;
72 else
73 *value = tmp16 & 0xff;
74 } else {
75 pci_config_read8((u8 *)addr, &tmp8);
76 *value = (u32) tmp8;
77 }
78 break;
79
80 case 2:
81 if (where < 8) {
82 pci_config_read16((u16 *)addr, &tmp16);
83 *value = (u32) tmp16;
84 } else {
85 pci_config_read8((u8 *)addr, &tmp8);
86 *value = (u32) tmp8;
87 pci_config_read8(((u8 *)addr) + 1, &tmp8);
88 *value |= ((u32) tmp8) << 8;
89 }
90 break;
91
92 case 4:
93 tmp32 = 0xffffffff;
94 sun4u_read_pci_cfg_host(pbm, bus, devfn,
95 where, 2, &tmp32);
96 *value = tmp32;
97
98 tmp32 = 0xffffffff;
99 sun4u_read_pci_cfg_host(pbm, bus, devfn,
100 where + 2, 2, &tmp32);
101 *value |= tmp32 << 16;
102 break;
103 }
104 return PCIBIOS_SUCCESSFUL;
105}
106
107static int sun4u_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
108 int where, int size, u32 *value)
109{
110 struct pci_pbm_info *pbm = bus_dev->sysdata;
111 unsigned char bus = bus_dev->number;
112 u32 *addr;
113 u16 tmp16;
114 u8 tmp8;
115
116 switch (size) {
117 case 1:
118 *value = 0xff;
119 break;
120 case 2:
121 *value = 0xffff;
122 break;
123 case 4:
124 *value = 0xffffffff;
125 break;
126 }
127
128 if (!bus_dev->number && !PCI_SLOT(devfn))
129 return sun4u_read_pci_cfg_host(pbm, bus, devfn, where,
130 size, value);
131
132 addr = sun4u_config_mkaddr(pbm, bus, devfn, where);
133 if (!addr)
134 return PCIBIOS_SUCCESSFUL;
135
136 switch (size) {
137 case 1:
138 pci_config_read8((u8 *)addr, &tmp8);
139 *value = (u32) tmp8;
140 break;
141
142 case 2:
143 if (where & 0x01) {
144 printk("pci_read_config_word: misaligned reg [%x]\n",
145 where);
146 return PCIBIOS_SUCCESSFUL;
147 }
148 pci_config_read16((u16 *)addr, &tmp16);
149 *value = (u32) tmp16;
150 break;
151
152 case 4:
153 if (where & 0x03) {
154 printk("pci_read_config_dword: misaligned reg [%x]\n",
155 where);
156 return PCIBIOS_SUCCESSFUL;
157 }
158 pci_config_read32(addr, value);
159 break;
160 }
161 return PCIBIOS_SUCCESSFUL;
162}
163
164static int sun4u_write_pci_cfg_host(struct pci_pbm_info *pbm,
165 unsigned char bus, unsigned int devfn,
166 int where, int size, u32 value)
167{
168 u32 *addr;
169
170 addr = sun4u_config_mkaddr(pbm, bus, devfn, where);
171 if (!addr)
172 return PCIBIOS_SUCCESSFUL;
173
174 switch (size) {
175 case 1:
176 if (where < 8) {
177 unsigned long align = (unsigned long) addr;
178 u16 tmp16;
179
180 align &= ~1;
181 pci_config_read16((u16 *)align, &tmp16);
182 if (where & 1) {
183 tmp16 &= 0x00ff;
184 tmp16 |= value << 8;
185 } else {
186 tmp16 &= 0xff00;
187 tmp16 |= value;
188 }
189 pci_config_write16((u16 *)align, tmp16);
190 } else
191 pci_config_write8((u8 *)addr, value);
192 break;
193 case 2:
194 if (where < 8) {
195 pci_config_write16((u16 *)addr, value);
196 } else {
197 pci_config_write8((u8 *)addr, value & 0xff);
198 pci_config_write8(((u8 *)addr) + 1, value >> 8);
199 }
200 break;
201 case 4:
202 sun4u_write_pci_cfg_host(pbm, bus, devfn,
203 where, 2, value & 0xffff);
204 sun4u_write_pci_cfg_host(pbm, bus, devfn,
205 where + 2, 2, value >> 16);
206 break;
207 }
208 return PCIBIOS_SUCCESSFUL;
209}
210
211static int sun4u_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
212 int where, int size, u32 value)
213{
214 struct pci_pbm_info *pbm = bus_dev->sysdata;
215 unsigned char bus = bus_dev->number;
216 u32 *addr;
217
218 if (!bus_dev->number && !PCI_SLOT(devfn))
219 return sun4u_write_pci_cfg_host(pbm, bus, devfn, where,
220 size, value);
221
222 addr = sun4u_config_mkaddr(pbm, bus, devfn, where);
223 if (!addr)
224 return PCIBIOS_SUCCESSFUL;
225
226 switch (size) {
227 case 1:
228 pci_config_write8((u8 *)addr, value);
229 break;
230
231 case 2:
232 if (where & 0x01) {
233 printk("pci_write_config_word: misaligned reg [%x]\n",
234 where);
235 return PCIBIOS_SUCCESSFUL;
236 }
237 pci_config_write16((u16 *)addr, value);
238 break;
239
240 case 4:
241 if (where & 0x03) {
242 printk("pci_write_config_dword: misaligned reg [%x]\n",
243 where);
244 return PCIBIOS_SUCCESSFUL;
245 }
246 pci_config_write32(addr, value);
247 }
248 return PCIBIOS_SUCCESSFUL;
249}
250
251struct pci_ops sun4u_pci_ops = {
252 .read = sun4u_read_pci_cfg,
253 .write = sun4u_write_pci_cfg,
254};
255
256static int sun4v_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
257 int where, int size, u32 *value)
258{
259 struct pci_pbm_info *pbm = bus_dev->sysdata;
260 u32 devhandle = pbm->devhandle;
261 unsigned int bus = bus_dev->number;
262 unsigned int device = PCI_SLOT(devfn);
263 unsigned int func = PCI_FUNC(devfn);
264 unsigned long ret;
265
266 if (config_out_of_range(pbm, bus, devfn, where)) {
267 ret = ~0UL;
268 } else {
269 ret = pci_sun4v_config_get(devhandle,
270 HV_PCI_DEVICE_BUILD(bus, device, func),
271 where, size);
272 }
273 switch (size) {
274 case 1:
275 *value = ret & 0xff;
276 break;
277 case 2:
278 *value = ret & 0xffff;
279 break;
280 case 4:
281 *value = ret & 0xffffffff;
282 break;
283 }
284
285
286 return PCIBIOS_SUCCESSFUL;
287}
288
289static int sun4v_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
290 int where, int size, u32 value)
291{
292 struct pci_pbm_info *pbm = bus_dev->sysdata;
293 u32 devhandle = pbm->devhandle;
294 unsigned int bus = bus_dev->number;
295 unsigned int device = PCI_SLOT(devfn);
296 unsigned int func = PCI_FUNC(devfn);
297
298 if (config_out_of_range(pbm, bus, devfn, where)) {
299 /* Do nothing. */
300 } else {
301 /* We don't check for hypervisor errors here, but perhaps
302 * we should and influence our return value depending upon
303 * what kind of error is thrown.
304 */
305 pci_sun4v_config_put(devhandle,
306 HV_PCI_DEVICE_BUILD(bus, device, func),
307 where, size, value);
308 }
309 return PCIBIOS_SUCCESSFUL;
310}
311
312struct pci_ops sun4v_pci_ops = {
313 .read = sun4v_read_pci_cfg,
314 .write = sun4v_write_pci_cfg,
315};
316
317void pci_get_pbm_props(struct pci_pbm_info *pbm)
318{
319 const u32 *val = of_get_property(pbm->op->dev.of_node, "bus-range", NULL);
320
321 pbm->pci_first_busno = val[0];
322 pbm->pci_last_busno = val[1];
323
324 val = of_get_property(pbm->op->dev.of_node, "ino-bitmap", NULL);
325 if (val) {
326 pbm->ino_bitmap = (((u64)val[1] << 32UL) |
327 ((u64)val[0] << 0UL));
328 }
329}
330
331static void pci_register_legacy_regions(struct resource *io_res,
332 struct resource *mem_res)
333{
334 struct resource *p;
335
336 /* VGA Video RAM. */
337 p = kzalloc(sizeof(*p), GFP_KERNEL);
338 if (!p)
339 return;
340
341 p->name = "Video RAM area";
342 p->start = mem_res->start + 0xa0000UL;
343 p->end = p->start + 0x1ffffUL;
344 p->flags = IORESOURCE_BUSY;
345 request_resource(mem_res, p);
346
347 p = kzalloc(sizeof(*p), GFP_KERNEL);
348 if (!p)
349 return;
350
351 p->name = "System ROM";
352 p->start = mem_res->start + 0xf0000UL;
353 p->end = p->start + 0xffffUL;
354 p->flags = IORESOURCE_BUSY;
355 request_resource(mem_res, p);
356
357 p = kzalloc(sizeof(*p), GFP_KERNEL);
358 if (!p)
359 return;
360
361 p->name = "Video ROM";
362 p->start = mem_res->start + 0xc0000UL;
363 p->end = p->start + 0x7fffUL;
364 p->flags = IORESOURCE_BUSY;
365 request_resource(mem_res, p);
366}
367
368static void pci_register_iommu_region(struct pci_pbm_info *pbm)
369{
370 const u32 *vdma = of_get_property(pbm->op->dev.of_node, "virtual-dma",
371 NULL);
372
373 if (vdma) {
374 struct resource *rp = kzalloc(sizeof(*rp), GFP_KERNEL);
375
376 if (!rp) {
377 pr_info("%s: Cannot allocate IOMMU resource.\n",
378 pbm->name);
379 return;
380 }
381 rp->name = "IOMMU";
382 rp->start = pbm->mem_space.start + (unsigned long) vdma[0];
383 rp->end = rp->start + (unsigned long) vdma[1] - 1UL;
384 rp->flags = IORESOURCE_BUSY;
385 if (request_resource(&pbm->mem_space, rp)) {
386 pr_info("%s: Unable to request IOMMU resource.\n",
387 pbm->name);
388 kfree(rp);
389 }
390 }
391}
392
393void pci_determine_mem_io_space(struct pci_pbm_info *pbm)
394{
395 const struct linux_prom_pci_ranges *pbm_ranges;
396 int i, saw_mem, saw_io;
397 int num_pbm_ranges;
398
399 saw_mem = saw_io = 0;
400 pbm_ranges = of_get_property(pbm->op->dev.of_node, "ranges", &i);
401 if (!pbm_ranges) {
402 prom_printf("PCI: Fatal error, missing PBM ranges property "
403 " for %s\n",
404 pbm->name);
405 prom_halt();
406 }
407
408 num_pbm_ranges = i / sizeof(*pbm_ranges);
409 memset(&pbm->mem64_space, 0, sizeof(struct resource));
410
411 for (i = 0; i < num_pbm_ranges; i++) {
412 const struct linux_prom_pci_ranges *pr = &pbm_ranges[i];
413 unsigned long a, size;
414 u32 parent_phys_hi, parent_phys_lo;
415 u32 size_hi, size_lo;
416 int type;
417
418 parent_phys_hi = pr->parent_phys_hi;
419 parent_phys_lo = pr->parent_phys_lo;
420 if (tlb_type == hypervisor)
421 parent_phys_hi &= 0x0fffffff;
422
423 size_hi = pr->size_hi;
424 size_lo = pr->size_lo;
425
426 type = (pr->child_phys_hi >> 24) & 0x3;
427 a = (((unsigned long)parent_phys_hi << 32UL) |
428 ((unsigned long)parent_phys_lo << 0UL));
429 size = (((unsigned long)size_hi << 32UL) |
430 ((unsigned long)size_lo << 0UL));
431
432 switch (type) {
433 case 0:
434 /* PCI config space, 16MB */
435 pbm->config_space = a;
436 break;
437
438 case 1:
439 /* 16-bit IO space, 16MB */
440 pbm->io_space.start = a;
441 pbm->io_space.end = a + size - 1UL;
442 pbm->io_space.flags = IORESOURCE_IO;
443 saw_io = 1;
444 break;
445
446 case 2:
447 /* 32-bit MEM space, 2GB */
448 pbm->mem_space.start = a;
449 pbm->mem_space.end = a + size - 1UL;
450 pbm->mem_space.flags = IORESOURCE_MEM;
451 saw_mem = 1;
452 break;
453
454 case 3:
455 /* 64-bit MEM handling */
456 pbm->mem64_space.start = a;
457 pbm->mem64_space.end = a + size - 1UL;
458 pbm->mem64_space.flags = IORESOURCE_MEM;
459 saw_mem = 1;
460 break;
461
462 default:
463 break;
464 }
465 }
466
467 if (!saw_io || !saw_mem) {
468 prom_printf("%s: Fatal error, missing %s PBM range.\n",
469 pbm->name,
470 (!saw_io ? "IO" : "MEM"));
471 prom_halt();
472 }
473
474 printk("%s: PCI IO[%llx] MEM[%llx]",
475 pbm->name,
476 pbm->io_space.start,
477 pbm->mem_space.start);
478 if (pbm->mem64_space.flags)
479 printk(" MEM64[%llx]",
480 pbm->mem64_space.start);
481 printk("\n");
482
483 pbm->io_space.name = pbm->mem_space.name = pbm->name;
484 pbm->mem64_space.name = pbm->name;
485
486 request_resource(&ioport_resource, &pbm->io_space);
487 request_resource(&iomem_resource, &pbm->mem_space);
488 if (pbm->mem64_space.flags)
489 request_resource(&iomem_resource, &pbm->mem64_space);
490
491 pci_register_legacy_regions(&pbm->io_space,
492 &pbm->mem_space);
493 pci_register_iommu_region(pbm);
494}
495
496/* Generic helper routines for PCI error reporting. */
497void pci_scan_for_target_abort(struct pci_pbm_info *pbm,
498 struct pci_bus *pbus)
499{
500 struct pci_dev *pdev;
501 struct pci_bus *bus;
502
503 list_for_each_entry(pdev, &pbus->devices, bus_list) {
504 u16 status, error_bits;
505
506 pci_read_config_word(pdev, PCI_STATUS, &status);
507 error_bits =
508 (status & (PCI_STATUS_SIG_TARGET_ABORT |
509 PCI_STATUS_REC_TARGET_ABORT));
510 if (error_bits) {
511 pci_write_config_word(pdev, PCI_STATUS, error_bits);
512 printk("%s: Device %s saw Target Abort [%016x]\n",
513 pbm->name, pci_name(pdev), status);
514 }
515 }
516
517 list_for_each_entry(bus, &pbus->children, node)
518 pci_scan_for_target_abort(pbm, bus);
519}
520
521void pci_scan_for_master_abort(struct pci_pbm_info *pbm,
522 struct pci_bus *pbus)
523{
524 struct pci_dev *pdev;
525 struct pci_bus *bus;
526
527 list_for_each_entry(pdev, &pbus->devices, bus_list) {
528 u16 status, error_bits;
529
530 pci_read_config_word(pdev, PCI_STATUS, &status);
531 error_bits =
532 (status & (PCI_STATUS_REC_MASTER_ABORT));
533 if (error_bits) {
534 pci_write_config_word(pdev, PCI_STATUS, error_bits);
535 printk("%s: Device %s received Master Abort [%016x]\n",
536 pbm->name, pci_name(pdev), status);
537 }
538 }
539
540 list_for_each_entry(bus, &pbus->children, node)
541 pci_scan_for_master_abort(pbm, bus);
542}
543
544void pci_scan_for_parity_error(struct pci_pbm_info *pbm,
545 struct pci_bus *pbus)
546{
547 struct pci_dev *pdev;
548 struct pci_bus *bus;
549
550 list_for_each_entry(pdev, &pbus->devices, bus_list) {
551 u16 status, error_bits;
552
553 pci_read_config_word(pdev, PCI_STATUS, &status);
554 error_bits =
555 (status & (PCI_STATUS_PARITY |
556 PCI_STATUS_DETECTED_PARITY));
557 if (error_bits) {
558 pci_write_config_word(pdev, PCI_STATUS, error_bits);
559 printk("%s: Device %s saw Parity Error [%016x]\n",
560 pbm->name, pci_name(pdev), status);
561 }
562 }
563
564 list_for_each_entry(bus, &pbus->children, node)
565 pci_scan_for_parity_error(pbm, bus);
566}