Debian Codegraph @

common/pci_dev.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
4 *
5 * PCI devices.
6 *
7 * Very interesting docs:
8 * http://www.science.unitn.it/~fiorella/guidelinux/tlk/node72.html
9 * http://www.science.unitn.it/~fiorella/guidelinux/tlk/node76.html
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <sys/types.h>
17
18 #include "cpu.h"
19 #include "vm.h"
20 #include "dynamips.h"
21 #include "memory.h"
22 #include "device.h"
23
24 #define DEBUG_PCI 1
25
26 #define GET_PCI_ADDR(offset,mask) ((pci_bus->pci_addr >> offset) & mask)
27
28 /* Trigger a PCI device IRQ */
29 void pci_dev_trigger_irq(vm_instance_t *vm,struct pci_device *dev)
30 {
31 if (dev->irq != -1)
32 vm_set_irq(vm,dev->irq);
33 }
34
35 /* Clear a PCI device IRQ */
36 void pci_dev_clear_irq(vm_instance_t *vm,struct pci_device *dev)
37 {
38 if (dev->irq != -1)
39 vm_clear_irq(vm,dev->irq);
40 }
41
42 /* Swapping function */
43 static inline m_uint32_t pci_swap(m_uint32_t val,int swap)
44 {
45 return((swap) ? swap32(val) : val);
46 }
47
48 /* PCI bus lookup */
49 struct pci_bus *pci_bus_lookup(struct pci_bus *pci_bus_root,int bus)
50 {
51 struct pci_bus *next_bus,*cur_bus = pci_bus_root;
52 struct pci_bridge *bridge;
53
54 while(cur_bus != NULL) {
55 if (cur_bus->bus == bus)
56 return cur_bus;
57
58 /* Try busses behind PCI bridges */
59 next_bus = NULL;
60
61 for(bridge=cur_bus->bridge_list;bridge;bridge=bridge->next) {
62 /*
63 * Specific case: final bridge with no checking of secondary
64 * bus number. Dynamically programming.
65 */
66 if (bridge->skip_bus_check) {
67 pci_bridge_set_bus_info(bridge,cur_bus->bus,bus,bus);
68 bridge->skip_bus_check = FALSE;
69 return bridge->pci_bus;
70 }
71
72 if ((bus >= bridge->sec_bus) && (bus <= bridge->sub_bus)) {
73 next_bus = bridge->pci_bus;
74 break;
75 }
76 }
77
78 cur_bus = next_bus;
79 }
80
81 return NULL;
82 }
83
84 /* PCI device local lookup */
85 struct pci_device *pci_dev_lookup_local(struct pci_bus *pci_bus,
86 int device,int function)
87 {
88 struct pci_device *dev;
89
90 for(dev=pci_bus->dev_list;dev;dev=dev->next)
91 if ((dev->device == device) && (dev->function == function))
92 return dev;
93
94 return NULL;
95 }
96
97 /* PCI Device lookup */
98 struct pci_device *pci_dev_lookup(struct pci_bus *pci_bus_root,
99 int bus,int device,int function)
100 {
101 struct pci_bus *req_bus;
102
103 /* Find, try to find the request bus */
104 if (!(req_bus = pci_bus_lookup(pci_bus_root,bus)))
105 return NULL;
106
107 /* Walk through devices present on this bus */
108 return pci_dev_lookup_local(req_bus,device,function);
109 }
110
111 /* Handle the address register access */
112 void pci_dev_addr_handler(cpu_gen_t *cpu,struct pci_bus *pci_bus,
113 u_int op_type,int swap,m_uint64_t *data)
114 {
115 if (op_type == MTS_WRITE)
116 pci_bus->pci_addr = pci_swap(*data,swap);
117 else
118 *data = pci_swap(pci_bus->pci_addr,swap);
119 }
120
121 /*
122 * Handle the data register access.
123 *
124 * The address of requested register is first written at address 0xcf8
125 * (with pci_dev_addr_handler).
126 *
127 * The data is read/written at address 0xcfc.
128 */
129 void pci_dev_data_handler(cpu_gen_t *cpu,struct pci_bus *pci_bus,
130 u_int op_type,int swap,m_uint64_t *data)
131 {
132 struct pci_device *dev;
133 int bus,device,function,reg;
134
135 if (op_type == MTS_READ)
136 *data = 0x0;
137
138 /*
139 * http://www.mega-tokyo.com/osfaq2/index.php/PciSectionOfPentiumVme
140 *
141 * 31 : Enable Bit
142 * 30 - 24 : Reserved
143 * 23 - 16 : Bus Number
144 * 15 - 11 : Device Number
145 * 10 - 8 : Function Number
146 * 7 - 2 : Register Number
147 * 1 - 0 : always 00
148 */
149 bus = GET_PCI_ADDR(16,0xff);
150 device = GET_PCI_ADDR(11,0x1f);
151 function = GET_PCI_ADDR(8,0x7);
152 reg = GET_PCI_ADDR(0,0xff);
153
154 /* Find the corresponding PCI device */
155 dev = pci_dev_lookup(pci_bus,bus,device,function);
156
157 if (!dev) {
158 if (op_type == MTS_READ) {
159 cpu_log(cpu,"PCI","read request for unknown device at pc=0x%llx "
160 "(bus=%d,device=%d,function=%d,reg=0x%2.2x).\n",
161 cpu_get_pc(cpu), bus, device, function, reg);
162 } else {
163 cpu_log(cpu,"PCI","write request (data=0x%8.8x) for unknown device "
164 "at pc=0x%llx (bus=%d,device=%d,function=%d,reg=0x%2.2x).\n",
165 pci_swap(*data,swap), cpu_get_pc(cpu),
166 bus, device, function, reg);
167 }
168
169 /* Returns an invalid device ID */
170 if ((op_type == MTS_READ) && (reg == PCI_REG_ID))
171 *data = 0xffffffff;
172 } else {
173 #if DEBUG_PCI
174 if (op_type == MTS_READ) {
175 cpu_log(cpu,"PCI","read request for device '%s' at pc=0x%llx: "
176 "bus=%d,device=%d,function=%d,reg=0x%2.2x\n",
177 dev->name, cpu_get_pc(cpu), bus, device, function, reg);
178 } else {
179 cpu_log(cpu,"PCI","write request (data=0x%8.8x) for device '%s' at pc=0x%llx: "
180 "bus=%d,device=%d,function=%d,reg=0x%2.2x\n",
181 pci_swap(*data,swap), dev->name, cpu_get_pc(cpu),
182 bus, device, function, reg);
183 }
184 #endif
185 if (op_type == MTS_WRITE) {
186 if (dev->write_register != NULL)
187 dev->write_register(cpu,dev,reg,pci_swap(*data,swap));
188 } else {
189 if (reg == PCI_REG_ID)
190 *data = pci_swap((dev->product_id << 16) | dev->vendor_id,swap);
191 else {
192 if (dev->read_register != NULL)
193 *data = pci_swap(dev->read_register(cpu,dev,reg),swap);
194 }
195 }
196 }
197 }
198
199 /* Add a PCI bridge */
200 struct pci_bridge *pci_bridge_add(struct pci_bus *pci_bus)
201 {
202 struct pci_bridge *bridge;
203
204 if (!pci_bus)
205 return NULL;
206
207 if (!(bridge = malloc(sizeof(*bridge)))) {
208 fprintf(stderr,"pci_bridge_add: unable to create new PCI bridge.\n");
209 return NULL;
210 }
211
212 memset(bridge,0,sizeof(*bridge));
213 bridge->pri_bus = pci_bus->bus;
214 bridge->sec_bus = -1;
215 bridge->sub_bus = -1;
216 bridge->pci_bus = NULL;
217
218 /* Insert the bridge in the double-linked list */
219 bridge->next = pci_bus->bridge_list;
220 bridge->pprev = &pci_bus->bridge_list;
221
222 if (pci_bus->bridge_list != NULL)
223 pci_bus->bridge_list->pprev = &bridge->next;
224
225 pci_bus->bridge_list = bridge;
226 return bridge;
227 }
228
229 /* Remove a PCI bridge from the double-linked list */
230 static inline void pci_bridge_remove_from_list(struct pci_bridge *bridge)
231 {
232 if (bridge->next)
233 bridge->next->pprev = bridge->pprev;
234
235 if (bridge->pprev)
236 *(bridge->pprev) = bridge->next;
237 }
238
239 /* Remove a PCI bridge */
240 void pci_bridge_remove(struct pci_bridge *bridge)
241 {
242 if (bridge != NULL) {
243 pci_bridge_remove_from_list(bridge);
244 free(bridge);
245 }
246 }
247
248 /* Map secondary bus to a PCI bridge */
249 void pci_bridge_map_bus(struct pci_bridge *bridge,struct pci_bus *pci_bus)
250 {
251 if (bridge != NULL) {
252 bridge->pci_bus = pci_bus;
253
254 if (bridge->pci_bus != NULL)
255 bridge->pci_bus->bus = bridge->sec_bus;
256 }
257 }
258
259 /* Set PCI bridge bus info */
260 void pci_bridge_set_bus_info(struct pci_bridge *bridge,
261 int pri_bus,int sec_bus,int sub_bus)
262 {
263 if (bridge != NULL) {
264 bridge->pri_bus = pri_bus;
265 bridge->sec_bus = sec_bus;
266 bridge->sub_bus = sub_bus;
267
268 if (bridge->pci_bus != NULL)
269 bridge->pci_bus->bus = bridge->sec_bus;
270 }
271 }
272
273 /* Add a PCI device */
274 struct pci_device *
275 pci_dev_add(struct pci_bus *pci_bus,char *name,
276 u_int vendor_id,u_int product_id,
277 int device,int function,int irq,
278 void *priv_data,pci_init_t init,
279 pci_reg_read_t read_register,
280 pci_reg_write_t write_register)
281 {
282 struct pci_device *dev;
283
284 if (!pci_bus)
285 return NULL;
286
287 if ((dev = pci_dev_lookup_local(pci_bus,device,function)) != NULL) {
288 fprintf(stderr,"pci_dev_add: bus %s, device %d, function %d already "
289 "registered (device '%s').\n",
290 pci_bus->name,device,function,dev->name);
291 return NULL;
292 }
293
294 /* we can create safely the new device */
295 if (!(dev = malloc(sizeof(*dev)))) {
296 fprintf(stderr,"pci_dev_add: unable to create new PCI device.\n");
297 return NULL;
298 }
299
300 memset(dev,0,sizeof(*dev));
301 dev->name = name;
302 dev->vendor_id = vendor_id;
303 dev->product_id = product_id;
304 dev->pci_bus = pci_bus;
305 dev->device = device;
306 dev->function = function;
307 dev->irq = irq;
308 dev->priv_data = priv_data;
309 dev->init = init;
310 dev->read_register = read_register;
311 dev->write_register = write_register;
312
313 /* Insert the device in the double-linked list */
314 dev->next = pci_bus->dev_list;
315 dev->pprev = &pci_bus->dev_list;
316
317 if (pci_bus->dev_list != NULL)
318 pci_bus->dev_list->pprev = &dev->next;
319
320 pci_bus->dev_list = dev;
321
322 if (init) init(dev);
323 return dev;
324 }
325
326 /* Add a basic PCI device that just returns a Vendor/Product ID */
327 struct pci_device *
328 pci_dev_add_basic(struct pci_bus *pci_bus,
329 char *name,u_int vendor_id,u_int product_id,
330 int device,int function)
331 {
332 return(pci_dev_add(pci_bus,name,vendor_id,product_id,
333 device,function,-1,NULL,
334 NULL,NULL,NULL));
335 }
336
337 /* Remove a device from the double-linked list */
338 static inline void pci_dev_remove_from_list(struct pci_device *dev)
339 {
340 if (dev->next)
341 dev->next->pprev = dev->pprev;
342
343 if (dev->pprev)
344 *(dev->pprev) = dev->next;
345 }
346
347 /* Remove a PCI device */
348 void pci_dev_remove(struct pci_device *dev)
349 {
350 if (dev != NULL) {
351 pci_dev_remove_from_list(dev);
352 free(dev);
353 }
354 }
355
356 /* Remove a PCI device given its ID (bus,device,function) */
357 int pci_dev_remove_by_id(struct pci_bus *pci_bus,
358 int bus,int device,int function)
359 {
360 struct pci_device *dev;
361
362 if (!(dev = pci_dev_lookup(pci_bus,bus,device,function)))
363 return(-1);
364
365 pci_dev_remove(dev);
366 return(0);
367 }
368
369 /* Remove a PCI device given its name */
370 int pci_dev_remove_by_name(struct pci_bus *pci_bus,char *name)
371 {
372 struct pci_device *dev,*next;
373 int count = 0;
374
375 for(dev=pci_bus->dev_list;dev;dev=next) {
376 next = dev->next;
377
378 if (!strcmp(dev->name,name)) {
379 pci_dev_remove(dev);
380 count++;
381 }
382 }
383
384 return(count);
385 }
386
387 /* Create a PCI bus */
388 struct pci_bus *pci_bus_create(char *name,int bus)
389 {
390 struct pci_bus *d;
391
392 if (!(d = malloc(sizeof(*d)))) {
393 fprintf(stderr,"pci_bus_create: unable to create PCI info.\n");
394 return NULL;
395 }
396
397 memset(d,0,sizeof(*d));
398 d->name = strdup(name);
399 d->bus = bus;
400 return d;
401 }
402
403 /* Delete a PCI bus */
404 void pci_bus_remove(struct pci_bus *pci_bus)
405 {
406 struct pci_device *dev,*next;
407 struct pci_bridge *bridge,*next_bridge;
408
409 if (pci_bus) {
410 /* Remove all devices */
411 for(dev=pci_bus->dev_list;dev;dev=next) {
412 next = dev->next;
413 free(dev);
414 }
415
416 /* Remove all bridges */
417 for(bridge=pci_bus->bridge_list;bridge;bridge=next_bridge) {
418 next_bridge = bridge->next;
419 free(bridge);
420 }
421
422 /* Free the structure itself */
423 free(pci_bus->name);
424 free(pci_bus);
425 }
426 }
427
428 /* Read a configuration register of a PCI bridge */
429 static m_uint32_t pci_bridge_read_reg(cpu_gen_t *cpu,struct pci_device *dev,
430 int reg)
431 {
432 struct pci_bridge *bridge = dev->priv_data;
433 m_uint32_t val = 0;
434
435 switch(reg) {
436 case 0x18:
437 return(bridge->cfg_reg_bus);
438 default:
439 if (bridge->fallback_read != NULL)
440 val = bridge->fallback_read(cpu,dev,reg);
441
442 /* Returns appropriate PCI bridge class code if nothing defined */
443 if ((reg == 0x08) && !val)
444 val = 0x06040000;
445
446 return(val);
447 }
448 }
449
450 /* Write a configuration register of a PCI bridge */
451 static void pci_bridge_write_reg(cpu_gen_t *cpu,struct pci_device *dev,
452 int reg,m_uint32_t value)
453 {
454 struct pci_bridge *bridge = dev->priv_data;
455 u_int pri_bus,sec_bus,sub_bus;
456
457 switch(reg) {
458 case 0x18:
459 bridge->cfg_reg_bus = value;
460 sub_bus = (value >> 16) & 0xFF;
461 sec_bus = (value >> 8) & 0xFF;
462 pri_bus = value & 0xFF;
463
464 /* Modify the PCI bridge settings */
465 vm_log(cpu->vm,"PCI",
466 "PCI bridge %d,%d,%d -> pri: %2.2u, sec: %2.2u, sub: %2.2u\n",
467 dev->pci_bus->bus,dev->device,dev->function,
468 pri_bus,sec_bus,sub_bus);
469
470 pci_bridge_set_bus_info(bridge,pri_bus,sec_bus,sub_bus);
471 break;
472
473 default:
474 if (bridge->fallback_write != NULL)
475 bridge->fallback_write(cpu,dev,reg,value);
476 }
477 }
478
479 /* Create a PCI bridge device */
480 struct pci_device *pci_bridge_create_dev(struct pci_bus *pci_bus,char *name,
481 u_int vendor_id,u_int product_id,
482 int device,int function,
483 struct pci_bus *sec_bus,
484 pci_reg_read_t fallback_read,
485 pci_reg_write_t fallback_write)
486 {
487 struct pci_bridge *bridge;
488 struct pci_device *dev;
489
490 /* Create the PCI bridge structure */
491 if (!(bridge = pci_bridge_add(pci_bus)))
492 return NULL;
493
494 /* Create the PCI device corresponding to the bridge */
495 dev = pci_dev_add(pci_bus,name,vendor_id,product_id,device,function,-1,
496 bridge,NULL,pci_bridge_read_reg,pci_bridge_write_reg);
497
498 if (!dev)
499 goto err_pci_dev;
500
501 /* Keep the associated PCI device for this bridge */
502 bridge->pci_dev = dev;
503
504 /* Set the fallback functions */
505 bridge->fallback_read = fallback_read;
506 bridge->fallback_write = fallback_write;
507
508 /* Map the secondary bus (disabled at startup) */
509 pci_bridge_map_bus(bridge,sec_bus);
510 return dev;
511
512 err_pci_dev:
513 pci_bridge_remove(bridge);
514 return NULL;
515 }
516
517 /* Show PCI device list of the specified bus */
518 static void pci_bus_show_dev_list(struct pci_bus *pci_bus)
519 {
520 struct pci_device *dev;
521 struct pci_bridge *bridge;
522 char bus_id[32];
523
524 if (!pci_bus)
525 return;
526
527 if (pci_bus->bus != -1) {
528 snprintf(bus_id,sizeof(bus_id),"%2d",pci_bus->bus);
529 } else {
530 strcpy(bus_id,"XX");
531 }
532
533 for(dev=pci_bus->dev_list;dev;dev=dev->next) {
534 printf(" %-18s: ID %4.4x:%4.4x, Bus %s, Dev. %2d, Func. %2d",
535 dev->name,dev->vendor_id,dev->product_id,
536 bus_id,dev->device,dev->function);
537
538 if (dev->irq != -1)
539 printf(", IRQ: %d\n",dev->irq);
540 else
541 printf("\n");
542 }
543
544 for(bridge=pci_bus->bridge_list;bridge;bridge=bridge->next)
545 pci_bus_show_dev_list(bridge->pci_bus);
546 }
547
548 /* Show PCI device list */
549 void pci_dev_show_list(struct pci_bus *pci_bus)
550 {
551 if (!pci_bus)
552 return;
553
554 printf("PCI Bus \"%s\" Device list:\n",pci_bus->name);
555 pci_bus_show_dev_list(pci_bus);
556 printf("\n");
557 }