Rev 4399 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4399 | Rev 4418 | ||
---|---|---|---|
1 | #include <malloc.h> |
1 | #include <malloc.h> |
2 | #include <assert.h> |
2 | #include <assert.h> |
3 | #include <unistd.h> |
3 | #include <unistd.h> |
4 | #include <ddi.h> |
4 | #include <ddi.h> |
5 | #include <libarch/ddi.h> |
5 | #include <libarch/ddi.h> |
6 | #include <stdio.h> |
6 | #include <stdio.h> |
7 | #include <futex.h> |
7 | #include <futex.h> |
8 | 8 | ||
9 | #include "pci.h" |
9 | #include "pci.h" |
10 | #include "pci_bus.h" |
10 | #include "pci_bus.h" |
11 | 11 | ||
12 | #define PCI_CONF_OFFSET 0x001000000 |
12 | #define PCI_CONF_OFFSET 0x001000000 |
13 | #define PCI_CONF_SIZE 0x001000000 |
13 | #define PCI_CONF_SIZE 0x001000000 |
14 | 14 | ||
15 | /* |
15 | /* |
16 | * virtual address of specified PCI configuration register: |
16 | * virtual address of specified PCI configuration register: |
17 | * conf_base ... base address of configuration address space |
17 | * conf_base ... base address of configuration address space |
18 | * bus ... bus number |
18 | * bus ... bus number |
19 | * dev ... device number (0 - 31) |
19 | * dev ... device number (0 - 31) |
20 | * fn ... function number (0 - 7) |
20 | * fn ... function number (0 - 7) |
21 | * reg ... register number (register's position within PCI configuration header) |
21 | * reg ... register number (register's position within PCI configuration header) |
22 | **/ |
22 | **/ |
23 | #define CONF_ADDR(conf_base, bus, dev, fn, reg) ((void *)(conf_base + ((bus << 16) | (dev << 11) | (fn << 8) | reg))) |
23 | #define CONF_ADDR(conf_base, bus, dev, fn, reg) ((void *)(conf_base + ((bus << 16) | (dev << 11) | (fn << 8) | reg))) |
24 | 24 | ||
25 | static atomic_t pci_conf_futex = FUTEX_INITIALIZER; |
25 | static atomic_t pci_conf_futex = FUTEX_INITIALIZER; |
26 | 26 | ||
27 | static long u2p_space_cnt = 0; |
27 | static long u2p_space_cnt = 0; |
28 | static long *u2p_bases; |
28 | static long *u2p_bases; |
29 | /* virtual addresses of PCI configuration spaces */ |
29 | /* virtual addresses of PCI configuration spaces */ |
30 | static void **conf_bases; |
30 | static void **conf_bases; |
31 | 31 | ||
32 | static int psycho_init(); |
32 | static int psycho_init(); |
33 | static void u2p_bases_init(); |
33 | static void u2p_bases_init(); |
34 | static void psycho_scan(); |
34 | static void psycho_scan(); |
35 | static void * psycho_conf_addr(pci_dev_t *dev, int reg); |
35 | static void * psycho_conf_addr(pci_dev_t *dev, int reg); |
36 | static void psycho_conf_read(pci_dev_t *d, int reg, uint8_t *buf, int len); |
36 | static void psycho_conf_read(pci_dev_t *d, int reg, uint8_t *buf, int len); |
37 | static void psycho_conf_write(pci_dev_t *d, int reg, uint8_t *buf, int len); |
37 | static void psycho_conf_write(pci_dev_t *d, int reg, uint8_t *buf, int len); |
38 | 38 | ||
39 | 39 | ||
40 | static void * psycho_conf_addr(pci_dev_t *dev, int reg) |
40 | static void * psycho_conf_addr(pci_dev_t *dev, int reg) |
41 | { |
41 | { |
42 | return CONF_ADDR(dev->bus->data, dev->bus->num, dev->dev, dev->fn, reg); |
42 | return CONF_ADDR(dev->bus->data, dev->bus->num, dev->dev, dev->fn, reg); |
43 | } |
43 | } |
44 | 44 | ||
45 | static void psycho_scan() |
45 | static void psycho_scan() |
46 | { |
46 | { |
47 | printf("PCI: psycho_scan\n"); |
47 | printf("PCI: psycho_scan\n"); |
48 | int i, num; |
48 | int i, num; |
49 | for (i = 0; i < u2p_space_cnt; i++) { |
49 | for (i = 0; i < u2p_space_cnt; i++) { |
50 | for (num = 0; num <= 0x80; num += 0x80) { |
50 | for (num = 0; num <= 0x80; num += 0x80) { |
51 | pci_bus_t *bus = pci_alloc_bus(); |
51 | pci_bus_t *bus = pci_alloc_bus(); |
52 | bus->num = num; |
52 | bus->num = num; |
53 | bus->data = conf_bases[i]; |
53 | bus->data = conf_bases[i]; |
54 | pci_bus_register(bus); |
54 | pci_bus_register(bus); |
55 | pci_bus_scan(bus); |
55 | pci_bus_scan(bus); |
56 | } |
56 | } |
57 | } |
57 | } |
58 | } |
58 | } |
59 | 59 | ||
60 | static int psycho_init() |
60 | static int psycho_init() |
61 | { |
61 | { |
62 | printf("PCI: starting psycho initialization.\n"); |
62 | printf("PCI: starting psycho initialization.\n"); |
63 | u2p_bases_init(); |
63 | u2p_bases_init(); |
64 | conf_bases = (void **)malloc(u2p_space_cnt * sizeof(void *)); |
64 | conf_bases = (void **)malloc(u2p_space_cnt * sizeof(void *)); |
65 | int i, error; |
65 | int i, error; |
66 | for (i = 0; i < u2p_space_cnt; i++) { |
66 | for (i = 0; i < u2p_space_cnt; i++) { |
67 | if (error = pio_enable((void *)(u2p_bases[i] + PCI_CONF_OFFSET), PCI_CONF_SIZE, &(conf_bases[i]))) { |
67 | if (error = pio_enable((void *)(u2p_bases[i] + PCI_CONF_OFFSET), PCI_CONF_SIZE, &(conf_bases[i]))) { |
68 | printf("PCI: failed to enable psycho conf. adr. space num. %d. (error %d)\n", i, error); |
68 | printf("PCI: failed to enable psycho conf. adr. space num. %d. (error %d)\n", i, error); |
69 | return 0; |
69 | return 0; |
70 | } |
70 | } |
71 | } |
71 | } |
72 | return 1; |
72 | return 1; |
73 | } |
73 | } |
74 | 74 | ||
75 | static void u2p_bases_init() |
75 | static void u2p_bases_init() |
76 | { |
76 | { |
77 | // TODO: write this more generally - get this information from firmware (using kernel + sysinfo) |
77 | // TODO: write this more generally - get this information from firmware (using kernel + sysinfo) |
78 | u2p_space_cnt = 2; |
78 | u2p_space_cnt = 2; |
79 | //u2p_space_cnt = 1; |
79 | //u2p_space_cnt = 1; |
80 | u2p_bases = (void **)malloc(u2p_space_cnt * sizeof(void *)); |
80 | u2p_bases = (void **)malloc(u2p_space_cnt * sizeof(void *)); |
81 | u2p_bases[0] = 0x1c800000000; |
81 | u2p_bases[0] = 0x1c800000000; |
82 | u2p_bases[1] = 0x1ca00000000; |
82 | u2p_bases[1] = 0x1ca00000000; |
83 | } |
83 | } |
84 | 84 | ||
85 | uint8_t pci_conf_read_8(pci_dev_t *dev, int reg) |
85 | uint8_t pci_conf_read_8(pci_dev_t *dev, int reg) |
86 | { |
86 | { |
87 | uint8_t res; |
87 | uint8_t res; |
88 | futex_down(&pci_conf_futex); |
88 | futex_down(&pci_conf_futex); |
89 | psycho_conf_read(dev, reg, &res, sizeof(uint8_t)); |
89 | psycho_conf_read(dev, reg, &res, sizeof(uint8_t)); |
90 | futex_up(&pci_conf_futex); |
90 | futex_up(&pci_conf_futex); |
91 | return res; |
91 | return res; |
92 | } |
92 | } |
93 | 93 | ||
94 | uint16_t pci_conf_read_16(pci_dev_t *dev, int reg) |
94 | uint16_t pci_conf_read_16(pci_dev_t *dev, int reg) |
95 | { |
95 | { |
96 | uint16_t res; |
96 | uint16_t res; |
97 | futex_down(&pci_conf_futex); |
97 | futex_down(&pci_conf_futex); |
98 | psycho_conf_read(dev, reg, (uint8_t *)(&res), sizeof(uint16_t)); |
98 | psycho_conf_read(dev, reg, (uint8_t *)(&res), sizeof(uint16_t)); |
99 | futex_up(&pci_conf_futex); |
99 | futex_up(&pci_conf_futex); |
100 | return res; |
100 | return res; |
101 | } |
101 | } |
102 | 102 | ||
103 | uint32_t pci_conf_read_32(pci_dev_t *dev, int reg) |
103 | uint32_t pci_conf_read_32(pci_dev_t *dev, int reg) |
104 | { |
104 | { |
105 | uint32_t res; |
105 | uint32_t res; |
106 | futex_down(&pci_conf_futex); |
106 | futex_down(&pci_conf_futex); |
107 | psycho_conf_read(dev, reg, (uint8_t *)(&res), sizeof(uint32_t)); |
107 | psycho_conf_read(dev, reg, (uint8_t *)(&res), sizeof(uint32_t)); |
108 | futex_up(&pci_conf_futex); |
108 | futex_up(&pci_conf_futex); |
109 | return res; |
109 | return res; |
110 | } |
110 | } |
111 | 111 | ||
112 | static inline uint16_t invert_endianess_16(uint16_t x) |
112 | static inline uint16_t invert_endianness_16(uint16_t x) |
113 | { |
113 | { |
114 | return (x & 0xFF) << 8 | (x >> 8); |
114 | return (x & 0xFF) << 8 | (x >> 8); |
115 | } |
115 | } |
116 | 116 | ||
117 | static inline uint32_t invert_endianess_32(uint32_t x) |
117 | static inline uint32_t invert_endianness_32(uint32_t x) |
118 | { |
118 | { |
119 | return ((x & 0xFF) << 24) | ((x & 0xFF00) << 8) | ((x & 0xFF0000) >> 8) | (x >> 24); |
119 | return ((x & 0xFF) << 24) | ((x & 0xFF00) << 8) | ((x & 0xFF0000) >> 8) | (x >> 24); |
120 | } |
120 | } |
121 | 121 | ||
122 | static void psycho_conf_read(pci_dev_t *d, int reg, uint8_t *buf, int len) |
122 | static void psycho_conf_read(pci_dev_t *d, int reg, uint8_t *buf, int len) |
123 | { |
123 | { |
124 | switch (len) { |
124 | switch (len) { |
125 | case 1: |
125 | case 1: |
126 | buf[0] = pio_read_8(psycho_conf_addr(d, reg)); |
126 | buf[0] = pio_read_8(psycho_conf_addr(d, reg)); |
127 | break; |
127 | break; |
128 | case 2: |
128 | case 2: |
129 | *((uint16_t *)buf) = invert_endianess_16(pio_read_16(psycho_conf_addr(d, reg))); |
129 | *((uint16_t *)buf) = invert_endianness_16(pio_read_16(psycho_conf_addr(d, reg))); |
130 | break; |
130 | break; |
131 | case 4: |
131 | case 4: |
132 | *((uint32_t *)buf) = invert_endianess_32(pio_read_32(psycho_conf_addr(d, reg))); |
132 | *((uint32_t *)buf) = invert_endianness_32(pio_read_32(psycho_conf_addr(d, reg))); |
133 | break; |
133 | break; |
134 | } |
134 | } |
135 | } |
135 | } |
136 | 136 | ||
137 | static void psycho_conf_write(pci_dev_t *d, int reg, uint8_t *buf, int len) |
137 | static void psycho_conf_write(pci_dev_t *d, int reg, uint8_t *buf, int len) |
138 | { |
138 | { |
139 | switch (len) { |
139 | switch (len) { |
140 | case 1: |
140 | case 1: |
141 | pio_write_8(psycho_conf_addr(d, reg), buf[0]); |
141 | pio_write_8(psycho_conf_addr(d, reg), buf[0]); |
142 | break; |
142 | break; |
143 | case 2: |
143 | case 2: |
144 | pio_write_16(psycho_conf_addr(d, reg), invert_endianess_16(*((uint16_t *)buf))); |
144 | pio_write_16(psycho_conf_addr(d, reg), invert_endianness_16(*((uint16_t *)buf))); |
145 | break; |
145 | break; |
146 | case 4: |
146 | case 4: |
147 | pio_write_32(psycho_conf_addr(d, reg), invert_endianess_32(*((uint32_t *)buf))); |
147 | pio_write_32(psycho_conf_addr(d, reg), invert_endianness_32(*((uint32_t *)buf))); |
148 | break; |
148 | break; |
149 | } |
149 | } |
150 | } |
150 | } |
151 | 151 | ||
152 | /* pci bus structure initialization */ |
152 | /* pci bus structure initialization */ |
153 | void pci_init_bus_data(pci_bus_t *bus, pci_bus_t *parent) |
153 | void pci_init_bus_data(pci_bus_t *bus, pci_bus_t *parent) |
154 | { |
154 | { |
155 | if (parent != NULL) { |
155 | if (parent != NULL) { |
156 | bus->data = parent->data; |
156 | bus->data = parent->data; |
157 | } |
157 | } |
158 | } |
158 | } |
159 | 159 | ||
160 | int pci_bus_init() |
160 | int pci_bus_init() |
161 | { |
161 | { |
162 | if(!psycho_init()) { |
162 | if(!psycho_init()) { |
163 | return 0; |
163 | return 0; |
164 | } |
164 | } |
165 | psycho_scan(); |
165 | psycho_scan(); |
166 | return 1; |
166 | return 1; |
167 | } |
167 | } |
168 | 168 | ||
169 | void pci_bus_clean() |
169 | void pci_bus_clean() |
170 | { |
170 | { |
171 | free(u2p_bases); |
171 | free(u2p_bases); |
172 | free(conf_bases); |
172 | free(conf_bases); |
173 | } |
173 | } |