1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/kernel.h>
16#include <linux/pci.h>
17#include <linux/delay.h>
18#include <linux/string.h>
19#include <linux/init.h>
20#include <linux/capability.h>
21#include <linux/sched.h>
22#include <linux/errno.h>
23#include <linux/bootmem.h>
24#include <linux/irq.h>
25#include <linux/io.h>
26#include <linux/uaccess.h>
27#include <linux/export.h>
28
29#include <asm/processor.h>
30#include <asm/sections.h>
31#include <asm/byteorder.h>
32#include <asm/hv_driver.h>
33#include <hv/drv_pcie_rc_intf.h>
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59int __write_once tile_plx_gen1;
60
61static struct pci_controller controllers[TILE_NUM_PCIE];
62static int num_controllers;
63static int pci_scan_flags[TILE_NUM_PCIE];
64
65static struct pci_ops tile_cfg_ops;
66
67
68
69
70
71resource_size_t pcibios_align_resource(void *data, const struct resource *res,
72 resource_size_t size, resource_size_t align)
73{
74 return res->start;
75}
76EXPORT_SYMBOL(pcibios_align_resource);
77
78
79
80
81
82
83
84static int tile_pcie_open(int controller_id, int config_type)
85{
86 char filename[32];
87 int fd;
88
89 sprintf(filename, "pcie/%d/config%d", controller_id, config_type);
90
91 fd = hv_dev_open((HV_VirtAddr)filename, 0);
92
93 return fd;
94}
95
96
97
98
99
100static int tile_init_irqs(int controller_id, struct pci_controller *controller)
101{
102 char filename[32];
103 int fd;
104 int ret;
105 int x;
106 struct pcie_rc_config rc_config;
107
108 sprintf(filename, "pcie/%d/ctl", controller_id);
109 fd = hv_dev_open((HV_VirtAddr)filename, 0);
110 if (fd < 0) {
111 pr_err("PCI: hv_dev_open(%s) failed\n", filename);
112 return -1;
113 }
114 ret = hv_dev_pread(fd, 0, (HV_VirtAddr)(&rc_config),
115 sizeof(rc_config), PCIE_RC_CONFIG_MASK_OFF);
116 hv_dev_close(fd);
117 if (ret != sizeof(rc_config)) {
118 pr_err("PCI: wanted %zd bytes, got %d\n",
119 sizeof(rc_config), ret);
120 return -1;
121 }
122
123 controller->irq_base = rc_config.intr;
124
125 for (x = 0; x < 4; x++)
126 tile_irq_activate(rc_config.intr + x,
127 TILE_IRQ_HW_CLEAR);
128
129 if (rc_config.plx_gen1)
130 controller->plx_gen1 = 1;
131
132 return 0;
133}
134
135
136
137
138
139
140
141
142
143int __init tile_pci_init(void)
144{
145 int i;
146
147 pr_info("PCI: Searching for controllers...\n");
148
149
150 num_controllers = 0;
151
152
153
154 for (i = 0; i < TILE_NUM_PCIE; i++) {
155
156
157
158
159 if (pci_scan_flags[i] == 0) {
160 int hv_cfg_fd0 = -1;
161 int hv_cfg_fd1 = -1;
162 int hv_mem_fd = -1;
163 char name[32];
164 struct pci_controller *controller;
165
166
167
168
169
170 hv_cfg_fd0 = tile_pcie_open(i, 0);
171 if (hv_cfg_fd0 < 0)
172 continue;
173 hv_cfg_fd1 = tile_pcie_open(i, 1);
174 if (hv_cfg_fd1 < 0) {
175 pr_err("PCI: Couldn't open config fd to HV "
176 "for controller %d\n", i);
177 goto err_cont;
178 }
179
180 sprintf(name, "pcie/%d/mem", i);
181 hv_mem_fd = hv_dev_open((HV_VirtAddr)name, 0);
182 if (hv_mem_fd < 0) {
183 pr_err("PCI: Could not open mem fd to HV!\n");
184 goto err_cont;
185 }
186
187 pr_info("PCI: Found PCI controller #%d\n", i);
188
189 controller = &controllers[i];
190
191 controller->index = i;
192 controller->hv_cfg_fd[0] = hv_cfg_fd0;
193 controller->hv_cfg_fd[1] = hv_cfg_fd1;
194 controller->hv_mem_fd = hv_mem_fd;
195 controller->first_busno = 0;
196 controller->last_busno = 0xff;
197 controller->ops = &tile_cfg_ops;
198
199 num_controllers++;
200 continue;
201
202err_cont:
203 if (hv_cfg_fd0 >= 0)
204 hv_dev_close(hv_cfg_fd0);
205 if (hv_cfg_fd1 >= 0)
206 hv_dev_close(hv_cfg_fd1);
207 if (hv_mem_fd >= 0)
208 hv_dev_close(hv_mem_fd);
209 continue;
210 }
211 }
212
213
214
215
216
217 for (i = 0; i < num_controllers; i++) {
218 struct pci_controller *controller = &controllers[i];
219
220 if (controller->plx_gen1)
221 tile_plx_gen1 = 1;
222 }
223
224 return num_controllers;
225}
226
227
228
229
230
231static int tile_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
232{
233 struct pci_controller *controller =
234 (struct pci_controller *)dev->sysdata;
235 return (pin - 1) + controller->irq_base;
236}
237
238
239static void fixup_read_and_payload_sizes(void)
240{
241 struct pci_dev *dev = NULL;
242 int smallest_max_payload = 0x1;
243 int max_read_size = 0x2;
244 u16 new_values;
245
246
247 for_each_pci_dev(dev) {
248 u32 devcap;
249 int max_payload;
250
251 if (!pci_is_pcie(dev))
252 continue;
253
254 pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &devcap);
255 max_payload = devcap & PCI_EXP_DEVCAP_PAYLOAD;
256 if (max_payload < smallest_max_payload)
257 smallest_max_payload = max_payload;
258 }
259
260
261 new_values = (max_read_size << 12) | (smallest_max_payload << 5);
262 for_each_pci_dev(dev)
263 pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
264 PCI_EXP_DEVCTL_PAYLOAD | PCI_EXP_DEVCTL_READRQ,
265 new_values);
266}
267
268
269
270
271
272
273
274
275int __init pcibios_init(void)
276{
277 int i;
278
279 pr_info("PCI: Probing PCI hardware\n");
280
281
282
283
284
285
286 mdelay(250);
287
288
289 for (i = 0; i < TILE_NUM_PCIE; i++) {
290
291
292
293
294
295 if (pci_scan_flags[i] == 0 && controllers[i].ops != NULL) {
296 struct pci_controller *controller = &controllers[i];
297 struct pci_bus *bus;
298 LIST_HEAD(resources);
299
300 if (tile_init_irqs(i, controller)) {
301 pr_err("PCI: Could not initialize IRQs\n");
302 continue;
303 }
304
305 pr_info("PCI: initializing controller #%d\n", i);
306
307
308
309
310
311
312
313
314
315
316 pci_add_resource(&resources, &ioport_resource);
317 pci_add_resource(&resources, &iomem_resource);
318 bus = pci_scan_root_bus(NULL, 0, controller->ops, controller, &resources);
319 controller->root_bus = bus;
320 controller->last_busno = bus->busn_res.end;
321 }
322 }
323
324
325 pci_fixup_irqs(pci_common_swizzle, tile_map_irq);
326
327
328
329
330
331
332
333 pci_assign_unassigned_resources();
334
335
336 fixup_read_and_payload_sizes();
337
338
339 for (i = 0; i < TILE_NUM_PCIE; i++) {
340
341
342
343
344
345 if (pci_scan_flags[i] == 0 && controllers[i].ops != NULL) {
346 struct pci_bus *root_bus = controllers[i].root_bus;
347 struct pci_bus *next_bus;
348 struct pci_dev *dev;
349
350 list_for_each_entry(dev, &root_bus->devices, bus_list) {
351
352
353
354
355 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
356 (PCI_SLOT(dev->devfn) == 0)) {
357 next_bus = dev->subordinate;
358 controllers[i].mem_resources[0] =
359 *next_bus->resource[0];
360 controllers[i].mem_resources[1] =
361 *next_bus->resource[1];
362 controllers[i].mem_resources[2] =
363 *next_bus->resource[2];
364
365
366 pci_scan_flags[i] = 1;
367
368 break;
369 }
370 }
371 }
372 }
373
374 return 0;
375}
376subsys_initcall(pcibios_init);
377
378
379
380
381void pcibios_fixup_bus(struct pci_bus *bus)
382{
383
384}
385
386void pcibios_set_master(struct pci_dev *dev)
387{
388
389}
390
391
392
393
394
395
396
397
398int pcibios_enable_device(struct pci_dev *dev, int mask)
399{
400 u16 cmd, old_cmd;
401 u8 header_type;
402 int i;
403 struct resource *r;
404
405 pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
406
407 pci_read_config_word(dev, PCI_COMMAND, &cmd);
408 old_cmd = cmd;
409 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
410
411
412
413
414 cmd |= PCI_COMMAND_IO;
415 cmd |= PCI_COMMAND_MEMORY;
416 } else {
417
418
419
420
421 for (i = 0; i < 6; i++) {
422 r = &dev->resource[i];
423 if (r->flags & IORESOURCE_UNSET) {
424 pr_err("PCI: Device %s not available "
425 "because of resource collisions\n",
426 pci_name(dev));
427 return -EINVAL;
428 }
429 if (r->flags & IORESOURCE_IO)
430 cmd |= PCI_COMMAND_IO;
431 if (r->flags & IORESOURCE_MEM)
432 cmd |= PCI_COMMAND_MEMORY;
433 }
434 }
435
436
437
438
439 if (cmd != old_cmd)
440 pci_write_config_word(dev, PCI_COMMAND, cmd);
441 return 0;
442}
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460static int tile_cfg_read(struct pci_bus *bus, unsigned int devfn, int offset,
461 int size, u32 *val)
462{
463 struct pci_controller *controller = bus->sysdata;
464 int busnum = bus->number & 0xff;
465 int slot = (devfn >> 3) & 0x1f;
466 int function = devfn & 0x7;
467 u32 addr;
468 int config_mode = 1;
469
470
471
472
473
474
475
476
477 if (busnum == 0) {
478
479
480
481
482 if (slot) {
483 *val = 0xFFFFFFFF;
484 return 0;
485 }
486 config_mode = 0;
487 }
488
489 addr = busnum << 20;
490 addr |= slot << 15;
491 addr |= function << 12;
492 addr |= (offset & 0xFFF);
493
494 return hv_dev_pread(controller->hv_cfg_fd[config_mode], 0,
495 (HV_VirtAddr)(val), size, addr);
496}
497
498
499
500
501
502
503static int tile_cfg_write(struct pci_bus *bus, unsigned int devfn, int offset,
504 int size, u32 val)
505{
506 struct pci_controller *controller = bus->sysdata;
507 int busnum = bus->number & 0xff;
508 int slot = (devfn >> 3) & 0x1f;
509 int function = devfn & 0x7;
510 u32 addr;
511 int config_mode = 1;
512 HV_VirtAddr valp = (HV_VirtAddr)&val;
513
514
515
516
517 if (busnum == 0) {
518
519
520
521
522 if (slot)
523 return 0;
524 config_mode = 0;
525 }
526
527 addr = busnum << 20;
528 addr |= slot << 15;
529 addr |= function << 12;
530 addr |= (offset & 0xFFF);
531
532#ifdef __BIG_ENDIAN
533
534 valp += 4 - size;
535#endif
536
537 return hv_dev_pwrite(controller->hv_cfg_fd[config_mode], 0,
538 valp, size, addr);
539}
540
541
542static struct pci_ops tile_cfg_ops = {
543 .read = tile_cfg_read,
544 .write = tile_cfg_write,
545};
546
547
548
549
550
551
552
553
554
555
556
557
558#define TILE_READ(size, type) \
559type _tile_read##size(unsigned long addr) \
560{ \
561 type val; \
562 int idx = 0; \
563 if (addr > controllers[0].mem_resources[1].end && \
564 addr > controllers[0].mem_resources[2].end) \
565 idx = 1; \
566 if (hv_dev_pread(controllers[idx].hv_mem_fd, 0, \
567 (HV_VirtAddr)(&val), sizeof(type), addr)) \
568 pr_err("PCI: read %zd bytes at 0x%lX failed\n", \
569 sizeof(type), addr); \
570 return val; \
571} \
572EXPORT_SYMBOL(_tile_read##size)
573
574TILE_READ(b, u8);
575TILE_READ(w, u16);
576TILE_READ(l, u32);
577TILE_READ(q, u64);
578
579#define TILE_WRITE(size, type) \
580void _tile_write##size(type val, unsigned long addr) \
581{ \
582 int idx = 0; \
583 if (addr > controllers[0].mem_resources[1].end && \
584 addr > controllers[0].mem_resources[2].end) \
585 idx = 1; \
586 if (hv_dev_pwrite(controllers[idx].hv_mem_fd, 0, \
587 (HV_VirtAddr)(&val), sizeof(type), addr)) \
588 pr_err("PCI: write %zd bytes at 0x%lX failed\n", \
589 sizeof(type), addr); \
590} \
591EXPORT_SYMBOL(_tile_write##size)
592
593TILE_WRITE(b, u8);
594TILE_WRITE(w, u16);
595TILE_WRITE(l, u32);
596TILE_WRITE(q, u64);
597