usb.patch, with conflicts and some warnings fixed
[pintos-anon] / src / devices / pci.c
1 #define PCI_TRANSLATION_ENABLE 1
2
3 #include "devices/pci.h"
4 #include "threads/init.h"
5 #include "threads/malloc.h"
6 #include "threads/interrupt.h"
7 #include "threads/pte.h"
8 #include "threads/io.h"
9 #include "pci_lookup.h"
10 #include <round.h>
11 #include <list.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <stdio.h>
15
16 extern uint32_t *base_page_dir;
17
18 #define PCI_REG_ADDR    0xcf8
19 #define PCI_REG_DATA    0xcfc
20 #define pci_config_offset(bus, dev, func, reg)  (0x80000000 | ((bus) << 16) | ((dev) << 11) | ((func) << 8) | (reg & (~3)))
21
22 #define PCI_MAX_DEV_PER_BUS     32
23 #define PCI_MAX_FUNC_PER_DEV    8
24
25 #define PCI_HEADER_SZ           256
26
27 #define PCI_CMD_IO              0x001   /* enable io response */
28 #define PCI_CMD_MEMORY          0x002   /* enable memory response */
29 #define PCI_CMD_MASTER          0x004   /* enable bus mastering */
30 #define PCI_CMD_SPECIAL         0x008   /* enable special cycles */
31 #define PCI_CMD_INVALIDATE      0x010   /* memory write + invalidate */
32 #define PCI_CMD_PALETTE         0x020   /* palette snooping */
33 #define PCI_CMD_PARITY          0x040   /* parity checking */
34 #define PCI_CMD_WAIT            0x080   /* address/data stepping */
35 #define PCI_CMD_SERR            0x100   /* serr */
36 #define PCI_CMD_FASTBACK        0x200   /* back-to-back writing */
37 #define PCI_CMD_INTX_DISABLE    0x400   /* emulation disable */
38
39 #define PCI_STATUS_CAPLIST      0x10    /* capability list */
40 #define PCI_STATUS_66MHZ        0x20    /* 66mhz pci 2.1 bus */
41 #define PCI_STATUS_UDF          0x40    /* user definable features */
42 #define PCI_STATUS_FASTBACK     0x80    /* fast back to back */
43 #define PCI_STATUS_PARITY       0x100   /* parity error detected */
44 #define PCI_STATUS_DEVSEL       0x600   /* devsel mask */
45 #define PCI_STATUS_DEVSEL_FAST  0
46 #define PCI_STATUS_DEVSEL_MED   0x200
47 #define PCI_STATUS_DEVSEL_SLOW  0x400
48 #define PCI_STATUS_SIG_ABORT    0x0800  /* set on target abort */
49 #define PCI_STATUS_REC_ABORT    0x1000  /* master ack of abort */
50 #define PCI_STATUS_REC_ABORT_M  0x2000  /* set on master abort */
51 #define PCI_STATUS_SERR         0x4000  /* system error */
52 #define PCI_STATUS_PARITY2      0x8000  /* set on parity err */
53
54 #define PCI_HEADER_NORMAL       0
55 #define PCI_HEADER_BRIDGE       1
56 #define PCI_HEADER_CARDBUS      2
57
58 #define PCI_HEADER_MASK         0x7f
59 #define PCI_HEADER_MULTIFUNC    0x80
60
61 #define pci_get_reg_offset(x)   (&(((pci_config_header*)(NULL))->x))
62
63 #define PCI_VENDOR_INVALID      0xffff
64
65 #define PCI_BASEADDR_IO         0x00000001
66 #define PCI_BASEADDR_TYPEMASK   0x00000006
67 #define PCI_BASEADDR_32BIT      0x00000000
68 #define PCI_BASEADDR_64BIT      0x00000004
69 #define PCI_BASEADDR_PREFETCH   0x00000008
70 #define PCI_BASEADDR_ADDR32     0xfffffff0
71 #define PCI_BASEADDR_ADDR64     0xfffffffffffffff0
72 #define PCI_BASEADDR_IOPORT     0xfffffffc
73
74 #pragma pack(1)
75
76 struct pci_config_header
77 {
78   uint16_t pci_vendor_id;
79   uint16_t pci_device_id;
80
81   uint16_t pci_command;
82   uint16_t pci_status;
83
84   uint8_t pci_revision;
85
86   /* pci class */
87   uint8_t pci_interface;
88   uint8_t pci_minor;
89   uint8_t pci_major;
90
91   uint8_t pci_cachelinesz;
92   uint8_t pci_latency;
93   uint8_t pci_header;           /* header type */
94   uint8_t pci_bist;             /* self test */
95
96   uint32_t pci_base_reg[6];
97   uint32_t pci_cis;             /* cardbus cis pointer */
98   uint16_t pci_sub_vendor_id;
99   uint16_t pci_sub_id;
100   uint32_t pci_rom_addr;
101   uint8_t pci_capabilities;
102   uint8_t pci_resv[7];
103   uint8_t pci_int_line;
104   uint8_t pci_int_pin;
105   uint8_t pci_min_gnt;
106   uint8_t pci_max_latency;
107 };
108
109 #pragma pack()
110
111 #define PCI_BASE_COUNT          6
112 struct pci_dev
113 {
114   struct pci_config_header pch;
115   uint8_t bus, dev, func;
116   int base_reg_size[PCI_BASE_COUNT];
117
118   pci_handler_func *irq_handler;
119   void *irq_handler_aux;
120
121   struct list io_ranges;
122   struct list_elem peer;
123   struct list_elem int_peer;
124 };
125
126 enum pci_io_type
127 { PCI_IO_MEM, PCI_IO_PORT };
128
129 /* represents a PCI IO range */
130 struct pci_io
131 {
132   struct pci_dev *dev;
133   enum pci_io_type type;
134   size_t size;  /* bytes in range */
135   union
136   {
137     void *ptr;  /* virtual memory address */
138     int port;   /* io port */
139   } addr;
140   struct list_elem peer; /* linkage */
141 };
142
143
144 static void pci_write_config (int bus, int dev, int func, int reg,
145                               int size, uint32_t data);
146 static uint32_t pci_read_config (int bus, int dev, int func, int reg,
147                                  int size);
148 static void pci_read_all_config (int bus, int dev, int func,
149                                  struct pci_config_header *pch);
150 static int pci_scan_bus (int bus);
151 static int pci_probe (int bus, int dev, int func,
152                       struct pci_config_header *ph);
153 static int pci_pci_bridge (struct pci_dev *pd);
154 static void pci_power_on (struct pci_dev *pd);
155 static void pci_setup_io (struct pci_dev *pd);
156 static void pci_interrupt (struct intr_frame *);
157 static void pci_print_dev_info (struct pci_dev *pd);
158 static void *pci_alloc_mem (void *phys_ptr, int pages);
159
160 static struct list devices;
161 static struct list int_devices;
162
163 /* number of pages that have been allocated to pci devices in the pci zone */
164 static int num_pci_pages;
165
166 void
167 pci_init (void)
168 {
169   list_init (&devices);
170   list_init (&int_devices);
171
172   num_pci_pages = 0;
173
174   pci_scan_bus (0);
175   pci_print_stats ();
176 }
177
178 struct pci_dev *
179 pci_get_device (int vendor, int device, int func, int n)
180 {
181   struct list_elem *e;
182   int count;
183
184   count = 0;
185   e = list_begin (&devices);
186   while (e != list_end (&devices))
187     {
188       struct pci_dev *pd;
189
190       pd = list_entry (e, struct pci_dev, peer);
191       if (pd->pch.pci_vendor_id == vendor && pd->pch.pci_device_id == device)
192         if (pd->func == func)
193           {
194             if (count == n)
195               return pd;
196             count++;
197           }
198
199       e = list_next (e);
200     }
201
202   return NULL;
203 }
204
205 struct pci_dev *
206 pci_get_dev_by_class (int major, int minor, int iface, int n)
207 {
208   struct list_elem *e;
209   int count;
210
211   count = 0;
212   e = list_begin (&devices);
213   while (e != list_end (&devices))
214     {
215       struct pci_dev *pd;
216
217       pd = list_entry (e, struct pci_dev, peer);
218       if (pd->pch.pci_major == major && pd->pch.pci_minor == minor &&
219           pd->pch.pci_interface == iface)
220         {
221           if (count == n)
222             return pd;
223           count++;
224         }
225
226       e = list_next (e);
227     }
228
229   return NULL;
230
231 }
232
233 struct pci_io *
234 pci_io_enum (struct pci_dev *pio, struct pci_io *last)
235 {
236   struct list_elem *e;
237
238   if (last != NULL)
239     e = list_next (&last->peer);
240   else
241     e = list_begin (&pio->io_ranges);
242
243   if (e == list_end (&pio->io_ranges))
244     return NULL;
245
246   return list_entry (e, struct pci_io, peer);
247 }
248
249 void
250 pci_register_irq (struct pci_dev *pd, pci_handler_func * f, void *aux)
251 {
252   int int_vec;
253   ASSERT (pd != NULL);
254   ASSERT (pd->irq_handler == NULL);
255
256   pd->irq_handler_aux = aux;
257   pd->irq_handler = f;
258   int_vec = pd->pch.pci_int_line + 0x20;
259
260   list_push_back (&int_devices, &pd->int_peer);
261
262   /* ensure that pci interrupt is hooked */
263   if (!intr_is_registered (int_vec))
264     intr_register_ext (int_vec, pci_interrupt, "PCI");
265 }
266
267 void
268 pci_unregister_irq (struct pci_dev *pd)
269 {
270   ASSERT (pd != NULL);
271
272   intr_disable ();
273   list_remove (&pd->int_peer);
274   intr_enable ();
275
276   pd->irq_handler = NULL;
277   pd->irq_handler_aux = NULL;
278 }
279
280 size_t
281 pci_io_size (struct pci_io *pio)
282 {
283   ASSERT (pio != NULL);
284
285   return pio->size;
286 }
287
288 void
289 pci_reg_write32 (struct pci_io *pio, int reg, uint32_t data)
290 {
291   ASSERT (pio != NULL);
292   ASSERT ((unsigned) reg < pio->size);
293
294   if (pio->type == PCI_IO_MEM)
295     {
296       *((uint32_t *) (pio->addr.ptr + reg)) = data;
297     }
298   else if (pio->type == PCI_IO_PORT)
299     {
300       outl (pio->addr.port + reg, data);
301     }
302   else
303     PANIC ("pci: Invalid IO type\n");
304 }
305
306 void
307 pci_reg_write16 (struct pci_io *pio, int reg, uint16_t data)
308 {
309   ASSERT (pio != NULL);
310   ASSERT ((unsigned) reg < pio->size);
311
312   if (pio->type == PCI_IO_MEM)
313     {
314       *((uint16_t *) (pio->addr.ptr + reg)) = data;
315     }
316   else if (pio->type == PCI_IO_PORT)
317     {
318       outw (pio->addr.port + reg, data);
319     }
320   else
321     PANIC ("pci: Invalid IO type\n");
322 }
323
324 void
325 pci_reg_write8 (struct pci_io *pio, int reg, uint8_t data)
326 {
327   ASSERT (pio != NULL);
328   ASSERT ((unsigned) reg < pio->size);
329
330   if (pio->type == PCI_IO_MEM)
331     {
332       ((uint8_t *) pio->addr.ptr)[reg] = data;
333     }
334   else if (pio->type == PCI_IO_PORT)
335     {
336       outb (pio->addr.port + reg, data);
337     }
338   else
339     PANIC ("pci: Invalid IO type\n");
340 }
341
342 uint32_t
343 pci_reg_read32 (struct pci_io *pio, int reg)
344 {
345   uint32_t ret;
346
347   ASSERT (pio != NULL);
348   ASSERT ((unsigned) reg < pio->size);
349
350   if (pio->type == PCI_IO_MEM)
351     {
352       ret = ((uint32_t *) pio->addr.ptr)[reg];
353     }
354   else if (pio->type == PCI_IO_PORT)
355     {
356       ret = inl (pio->addr.port + reg);
357     }
358   else
359     PANIC ("pci: Invalid IO type\n");
360
361   return ret;
362 }
363
364 uint16_t
365 pci_reg_read16 (struct pci_io * pio, int reg)
366 {
367   uint16_t ret;
368
369   ASSERT (pio != NULL);
370   ASSERT ((unsigned) reg < pio->size);
371
372   ret = 0;
373   if (pio->type == PCI_IO_MEM)
374     {
375       ret = ((uint16_t *) pio->addr.ptr)[reg];
376     }
377   else if (pio->type == PCI_IO_PORT)
378     {
379       ret = inw (pio->addr.port + reg);
380     }
381   else
382     PANIC ("pci: Invalid IO type\n");
383
384   return ret;
385
386 }
387
388 uint8_t
389 pci_reg_read8 (struct pci_io * pio, int reg)
390 {
391   uint8_t ret;
392
393   ASSERT (pio != NULL);
394   ASSERT ((unsigned) reg < pio->size);
395
396   if (pio->type == PCI_IO_MEM)
397     {
398       ret = ((uint8_t *) pio->addr.ptr)[reg];
399     }
400   else if (pio->type == PCI_IO_PORT)
401     {
402       ret = inb (pio->addr.port + reg);
403     }
404   else
405     PANIC ("pci: Invalid IO type\n");
406
407   return ret;
408 }
409
410 void
411 pci_read_in (struct pci_io *pio UNUSED, int off UNUSED, size_t size UNUSED,
412              void *buf UNUSED)
413 {
414   PANIC ("pci_read_in: STUB");
415 }
416
417 void
418 pci_write_out (struct pci_io *pio UNUSED, int off UNUSED, size_t size UNUSED,
419                const void *buf UNUSED)
420 {
421   PANIC ("pci_write_out: STUB");
422 }
423
424 static void
425 pci_write_config (int bus, int dev, int func, int reg,
426                   int size, uint32_t data)
427 {
428   int config_offset;
429
430   config_offset = pci_config_offset (bus, dev, func, reg);
431
432   outl (PCI_REG_ADDR, config_offset);
433
434   switch (size)
435     {
436     case 1:
437       outb (PCI_REG_DATA + (reg & 3), data);
438       break;
439
440     case 2:
441       outw (PCI_REG_DATA + (reg & 3), data);
442       break;
443
444     case 4:
445       outl (PCI_REG_DATA, data);
446       break;
447     }
448 }
449
450 static uint32_t
451 pci_read_config (int bus, int dev, int func, int reg, int size)
452 {
453   uint32_t ret;
454   int config_offset;
455
456   config_offset = pci_config_offset (bus, dev, func, reg);
457
458   outl (PCI_REG_ADDR, config_offset);
459
460   switch (size)
461     {
462     case 1:
463       ret = inb (PCI_REG_DATA);
464       break;
465     case 2:
466       ret = inw (PCI_REG_DATA);
467       break;
468     case 4:
469       ret = inl (PCI_REG_DATA);
470       break;
471     default:
472       PANIC ("pci: Strange config read size\n");
473     }
474
475   return ret;
476 }
477
478 /* read entire configuration header into memory */
479 static void
480 pci_read_all_config (int bus, int dev, int func,
481                      struct pci_config_header *pch)
482 {
483   unsigned int i;
484   for (i = 0; i < ((sizeof (struct pci_config_header) + 3) & ~3) / 4; i++)
485     {
486       ((uint32_t *) pch)[i] = pci_read_config (bus, dev, func, i * 4, 4);
487     }
488 }
489
490
491 /** scan PCI bus for all devices */
492 static int
493 pci_scan_bus (int bus)
494 {
495   int dev;
496   int max_bus;
497
498   max_bus = 0;
499
500   for (dev = 0; dev < PCI_MAX_DEV_PER_BUS; dev++)
501     {
502       struct pci_config_header pch;
503       int func_cnt, func;
504
505       pci_read_all_config (bus, dev, 0, &pch);
506
507       if (pch.pci_vendor_id == PCI_VENDOR_INVALID)
508         continue;
509
510       func_cnt = 8;
511       if (!(pch.pci_header & PCI_HEADER_MULTIFUNC))
512         {
513           func_cnt = 1;
514         }
515
516       for (func = 0; func < func_cnt; func++)
517         {
518           int retbus;
519           retbus = pci_probe (bus, dev, func, &pch);
520           if (retbus > max_bus)
521             max_bus = retbus;
522         }
523     }
524
525   return max_bus;
526 }
527
528 /* get all information for a PCI device given bus/dev/func 
529    add pci device to device list if applicable 
530    return a new bus number if new bus is found
531  */
532 static int
533 pci_probe (int bus, int dev, int func, struct pci_config_header *ph)
534 {
535   struct pci_dev *pd;
536
537   if (func != 0)
538     {
539       pci_read_all_config (bus, dev, func, ph);
540       if (ph->pci_vendor_id == PCI_VENDOR_INVALID)
541         return bus;
542     }
543
544   pd = malloc (sizeof (struct pci_dev));
545   memcpy (&pd->pch, ph, sizeof (struct pci_config_header));
546   pd->irq_handler = NULL;
547   pd->irq_handler_aux = NULL;
548   pd->bus = bus;
549   pd->dev = dev;
550   pd->func = func;
551
552   list_init (&pd->io_ranges);
553   list_push_back (&devices, &pd->peer);
554
555
556   if (ph->pci_major == PCI_MAJOR_BRIDGE)
557     {
558       if (ph->pci_minor == PCI_MINOR_PCI)
559         return pci_pci_bridge (pd);
560     }
561
562   pci_setup_io (pd);
563   pci_power_on (pd);
564
565   return bus;
566 }
567
568 static void
569 pci_setup_io (struct pci_dev *pd)
570 {
571   int i;
572   for (i = 0; i < PCI_BASE_COUNT; i++)
573     {
574       uint32_t tmp;
575       struct pci_io *pio;
576
577       if (pd->pch.pci_base_reg[i] == 0)
578         {
579           continue;
580         }
581
582       /* determine io granularity.. */
583       pci_write_config (pd->bus, pd->dev, pd->func,
584                         offsetof (struct pci_config_header, pci_base_reg[i]),
585                         4, ~0);
586
587       tmp =
588         pci_read_config (pd->bus, pd->dev, pd->func,
589                          offsetof (struct pci_config_header, pci_base_reg[i]),
590                          4);
591
592       /* configure BAR to the default */
593       pci_write_config (pd->bus, pd->dev, pd->func,
594                         offsetof (struct pci_config_header, pci_base_reg[i]),
595                         4, pd->pch.pci_base_reg[i]);
596
597       pio = malloc (sizeof (struct pci_io));
598       pio->dev = pd;
599
600       if (tmp & PCI_BASEADDR_IO)
601         {
602           pio->type = PCI_IO_PORT;
603           pio->size = (uint16_t) ((~tmp + 1) & 0xffff) + 1;
604           pio->addr.port = pd->pch.pci_base_reg[i] & ~1;
605         }
606       else
607         {
608           uint32_t ofs;
609
610           pio->type = PCI_IO_MEM;
611           pio->size = ROUND_UP ((~tmp + 1), PGSIZE);
612           ofs = (pd->pch.pci_base_reg[i] & 0xfffffff0 & PGMASK);
613           pio->addr.ptr = pci_alloc_mem ((void *) pd->pch.pci_base_reg[i],
614                                          pio->size / PGSIZE);
615           if (pio->addr.ptr == NULL)
616             {
617               printf ("PCI: %d pages for %d:%d.%d failed - may not work\n",
618                       pio->size / PGSIZE, pd->bus, pd->dev, pd->func);
619               free (pio);
620               pio = NULL;
621             }
622           else
623             {
624               pio->addr.ptr = (void *) ((uintptr_t) pio->addr.ptr + ofs);
625             }
626         }
627
628       /* add IO struct to device, if valid */
629       if (pio != NULL)
630         list_push_back (&pd->io_ranges, &pio->peer);
631     }
632
633 }
634
635 static void
636 pci_power_on (struct pci_dev *pd UNUSED)
637 {
638   /* STUB */
639 }
640
641 static int
642 pci_pci_bridge (struct pci_dev *pd)
643 {
644   int max_bus;
645   uint16_t command;
646
647   /* put bus into offline mode */
648   command = pd->pch.pci_command;
649   command &= ~0x3;
650   pci_write_config (pd->bus, pd->dev, pd->func,
651                     offsetof (struct pci_config_header, pci_command),
652                     2, command);
653   pd->pch.pci_command = command;
654
655   /* set up primary bus */
656   pci_write_config (pd->bus, pd->dev, pd->func, 0x18, 1, pd->bus);
657   /* secondary bus */
658   pci_write_config (pd->bus, pd->dev, pd->func, 0x19, 1, pd->bus + 1);
659
660   /* disable subordinates */
661   pci_write_config (pd->bus, pd->dev, pd->func, 0x1a, 1, 0xff);
662
663   /* scan this new bus */
664   max_bus = pci_scan_bus (pd->bus + 1);
665
666   /* set subordinate to the actual last bus */
667   pci_write_config (pd->bus, pd->dev, pd->func, 0x1a, 1, max_bus);
668
669   /* put online */
670   command |= 0x03;
671   pci_write_config (pd->bus, pd->dev, pd->func,
672                     offsetof (struct pci_config_header, pci_command),
673                     2, command);
674   pd->pch.pci_command = command;
675
676   return max_bus;
677 }
678
679 /* alert all PCI devices waiting on interrupt line that IRQ fired */
680 static void
681 pci_interrupt (struct intr_frame *frame)
682 {
683   struct list_elem *e;
684   int int_line;
685
686   int_line = frame->vec_no - 0x20;
687   e = list_begin (&int_devices);
688   while (e != list_end (&int_devices))
689     {
690       struct pci_dev *pd;
691
692       pd = list_entry (e, struct pci_dev, int_peer);
693       if (pd->pch.pci_int_line == int_line)
694         pd->irq_handler (pd->irq_handler_aux);
695       e = list_next (e);
696     }
697 }
698
699 /* display information on all USB devices */
700 void
701 pci_print_stats (void)
702 {
703   struct list_elem *e;
704
705   e = list_begin (&devices);
706   while (e != list_end (&devices))
707     {
708       struct pci_dev *pd;
709
710       pd = list_entry (e, struct pci_dev, peer);
711       pci_print_dev_info (pd);
712
713       e = list_next (e);
714     }
715 }
716
717 static void
718 pci_print_dev_info (struct pci_dev *pd)
719 {
720   printf ("PCI Device %d:%d.%d (%x,%x): %s - %s (%s) IRQ %d\n",
721           pd->bus, pd->dev, pd->func,
722           pd->pch.pci_vendor_id,
723           pd->pch.pci_device_id,
724           pci_lookup_vendor (pd->pch.pci_vendor_id),
725           pci_lookup_device (pd->pch.pci_vendor_id, pd->pch.pci_device_id),
726           pci_lookup_class (pd->pch.pci_major, pd->pch.pci_minor,
727                             pd->pch.pci_interface), pd->pch.pci_int_line);
728 }
729
730 void
731 pci_mask_irq (struct pci_dev *pd)
732 {
733   intr_irq_mask (pd->pch.pci_int_line);
734 }
735
736 void
737 pci_unmask_irq (struct pci_dev *pd)
738 {
739   intr_irq_unmask (pd->pch.pci_int_line);
740 }
741
742 void
743 pci_write_config16 (struct pci_dev *pd, int off, uint16_t data)
744 {
745   pci_write_config (pd->bus, pd->dev, pd->func, off, 2, data);
746 }
747
748 void
749 pci_write_config32 (struct pci_dev *pd, int off, uint32_t data)
750 {
751   pci_write_config (pd->bus, pd->dev, pd->func, off, 4, data);
752 }
753
754 void
755 pci_write_config8 (struct pci_dev *pd, int off, uint8_t data)
756 {
757   pci_write_config (pd->bus, pd->dev, pd->func, off, 1, data);
758 }
759
760 uint8_t
761 pci_read_config8 (struct pci_dev *pd, int off)
762 {
763   return pci_read_config (pd->bus, pd->dev, pd->func, off, 1);
764 }
765
766 uint16_t
767 pci_read_config16 (struct pci_dev * pd, int off)
768 {
769   return pci_read_config (pd->bus, pd->dev, pd->func, off, 2);
770 }
771
772 uint32_t
773 pci_read_config32 (struct pci_dev * pd, int off)
774 {
775   return pci_read_config (pd->bus, pd->dev, pd->func, off, 4);
776 }
777
778
779 /** allocate PCI memory pages for PCI devices */
780 static void *
781 pci_alloc_mem (void *phys_ptr, int pages)
782 {
783   void *vaddr;
784   int i;
785
786   phys_ptr = (void *) ((uintptr_t) phys_ptr & ~PGMASK);
787
788   /* not enough space to allocate? */
789   if ((unsigned) (num_pci_pages + pages) >= (unsigned) PCI_ADDR_ZONE_PAGES)
790     {
791       return NULL;
792     }
793
794   /* insert into PCI_ZONE */
795   for (i = 0; i < pages; i++)
796     {
797       uint32_t pte_idx = (num_pci_pages + i) % 1024;
798       uint32_t pde_idx = (num_pci_pages + i) / 1024;
799       uint32_t *pt;
800       uint32_t pte;
801
802       pde_idx += pd_no ((void *) PCI_ADDR_ZONE_BEGIN);
803       pte = ((uint32_t) phys_ptr + (i * PGSIZE)) | PTE_P | PTE_W | PTE_CD;
804       pt = (uint32_t *) (ptov (init_page_dir[pde_idx] & ~PGMASK));
805       pt[pte_idx] = pte;
806     }
807
808   vaddr = (void *) (PCI_ADDR_ZONE_BEGIN + (num_pci_pages * PGSIZE));
809   num_pci_pages += pages;
810
811   return vaddr;
812 }