summaryrefslogtreecommitdiffstats
path: root/sys/dev/pci/pci.c
blob: 2b2debf68f0f981eabfb97d752d58f0b52f8ed5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
/**************************************************************************
**
**  $Id: pci.c,v 1.16 1995/02/25 17:26:22 se Exp $
**
**  General subroutines for the PCI bus on 80*86 systems.
**  pci_configure ()
**
**  386bsd / FreeBSD
**
**-------------------------------------------------------------------------
**
** Copyright (c) 1994 Wolfgang Stanglmeier.  All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
** 1. Redistributions of source code must retain the above copyright
**    notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
**    notice, this list of conditions and the following disclaimer in the
**    documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
**    derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
***************************************************************************
*/

#define PCI_PATCHLEVEL  "pl5 95/02/27"

#include <pci.h>
#if NPCI > 0

#ifndef __FreeBSD2__
#if __FreeBSD__ >= 2
#define	__FreeBSD2__
#endif
#endif

/*========================================================
**
**	#includes  and  declarations
**
**========================================================
*/

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/errno.h>
#include <sys/kernel.h>

#include <vm/vm.h>
#include <vm/vm_param.h>

#include <pci/pcivar.h>
#include <pci/pcireg.h>
#include <pci/pcibus.h>

#include <machine/pmap.h>
#ifdef __FreeBSD2__
#include <sys/devconf.h>

struct pci_devconf {
	struct kern_devconf pdc_kdc;
	struct pci_info     pdc_pi;
};

static int
pci_externalize (struct proc *, struct kern_devconf *, void *, size_t);
 
static int
pci_internalize (struct proc *, struct kern_devconf *, void *, size_t);
#else /* __FreeBSD2__ */

/*
**	Function prototypes missing in system headers
*/

extern	pmap_t pmap_kernel(void);
static	vm_offset_t pmap_mapdev (vm_offset_t paddr, vm_size_t vsize);
#endif /* __FreeBSD2__ */


/*========================================================
**
**	Autoconfiguration of pci devices.
**
**	This is reverse to the isa configuration.
**	(1) find a pci device.
**	(2) look for a driver.
**
**========================================================
*/

/*--------------------------------------------------------
**
**	Limit for pci bus numbers.
**
**--------------------------------------------------------
*/

#ifndef PCI_MAX_BUS
#define PCI_MAX_BUS	(256)
#endif

static	u_long	pci_bus_max = 1;

/*--------------------------------------------------------
**
**	The pci devices can be mapped to any address.
**	This is a list of possible starting addresses.
**	It can be prepended by a config option.
**
**--------------------------------------------------------
*/

static	u_long	pci_stable[] = {
#ifdef PCI_PMEM_START
	(PCI_PMEM_START),
#endif
	0xf1000000,
	0x53900000,
	0xc0000000,
	0x81000000,
	0x0f000000,
};

static	vm_offset_t pci_paddr = 0;
static	vm_offset_t pci_pold  = 0;
static	vm_offset_t pci_pidx  = 0;


/*--------------------------------------------------------
**
**	The pci ports can be mapped to any address.
**	As default we start at 0x400
**
**--------------------------------------------------------
*/

#ifndef PCI_PORT_START
#define PCI_PORT_START 0xbc00
#endif

static	u_short pci_ioaddr = PCI_PORT_START;

/*--------------------------------------------------------
**
**	The pci device interrupt lines should have been
**	assigned by the bios. But if the bios failed to
**	to it, we set it.
**
**--------------------------------------------------------
*/

#ifndef PCI_IRQ
#define PCI_IRQ 0
#endif

static	u_long      pci_irq   = PCI_IRQ;

/*---------------------------------------------------------
**
**	pci_configure ()
**
**	Probe all devices on pci bus and attach them.
**
**	May be called more than once.
**	Any device is attached only once.
**	(Attached devices are remembered in pci_seen.)
**	Has to take care of mirrored devices, which are
**	entailed by incomplete decoding of pci address lines.
**
**---------------------------------------------------------
*/

static void not_supported (pcici_t tag, u_long type);

static unsigned long pci_seen[PCI_MAX_BUS];

static int pci_conf_count;
static int pci_info_done;

void pci_configure()
{
	u_char	device,max_device;
	u_short	bus;
	pcici_t	tag;
	pcidi_t type, type8, type16;
	u_long	data;
	int	unit;
	int	pci_mechanism;
	int	pciint;
	int	irq;
	char*	name=0;
	u_short	old_ioaddr=pci_ioaddr;

	int	dvi;
	struct pci_device *dvp=0;

#ifdef __FreeBSD2__
	struct pci_devconf *pdcp;
#endif

	/*
	**	first check pci bus driver available
	*/

	if (pcibus_set.ls_length <= 0)
		return;

#define pcibus (*((struct pcibus*) pcibus_set.ls_items[0]))
	/*
	**	check pci bus present
	*/

	pci_mechanism = pcibus.pb_mode ();
	if (!pci_mechanism) return;
	max_device = pci_mechanism==1 ? 32 : 16;

	/*
	**	hello world ..
	*/

	pci_pold=pci_paddr;
	for (bus=0; bus<pci_bus_max; bus++) {
#ifndef PCI_QUIET
	    printf ("Probing for devices on the %s%d bus:\n",
	    	pcibus.pb_name, bus);
	    if (!pci_info_done) {
		pci_info_done=1;
		printf ("\tconfiguration mode %d allows %d devices.\n",
			pci_mechanism, max_device);
	    };
#endif
	    for (device=0; device<max_device; device ++) {

		if (pci_seen[bus] & (1ul << device))
			continue;

		tag  = pcibus.pb_tag (bus, device, 0);
		type = pcibus.pb_read (tag, PCI_ID_REG);

		if ((!type) || (type==0xfffffffful)) continue;

		/*
		**	lookup device in ioconfiguration:
		*/

        	for (dvi=0; dvi<pcidevice_set.ls_length; dvi++) {
			dvp = (struct pci_device*) pcidevice_set.ls_items[dvi];
			if ((name=(*dvp->pd_probe)(tag, type)))
				break;
			dvp = NULL;
		};

		/*
		**	check for mirrored devices.
		*/
		if (device & 0x08) {
			pcici_t	mtag;
			mtag  = pcibus.pb_tag (bus, device & ~0x08, 0);
			type8 = pcibus.pb_read (mtag, PCI_ID_REG);
		} else type8 = 0;
		if (device & 0x10) {
			pcici_t	mtag;
			mtag  = pcibus.pb_tag (bus, device & ~0x10, 0);
			type16 = pcibus.pb_read (mtag, PCI_ID_REG);
		} else type16 = 0;
		if ((type==type8) || (type==type16)) {
#ifndef PCI_QUIET
			if (dvp==NULL) continue;
			printf ("%s? <%s> mirrored on pci%d:%d\n",
				dvp->pd_name, name, bus, device);
#endif
			continue;
		};

		if (dvp==NULL) {
#ifndef PCI_QUIET
			if (pci_conf_count)
				continue;
			printf("%s%d:%d: ", pcibus.pb_name, bus, device);
			not_supported (tag, type);
#endif
			continue;
		};

		pci_seen[bus] |= (1ul << device);
		/*
		**	Get and increment the unit.
		*/

		unit = (*dvp->pd_count)++;

		/*
		**	ignore device ?
		*/

		if (!*name) continue;

		/*
		**	Announce this device
		*/

		printf ("%s%d <%s>", dvp->pd_name, unit, name);

		/*
		**	Get the int pin number (pci interrupt number a-d)
		**	from the pci configuration space.
		*/

		data = pcibus.pb_read (tag, PCI_INTERRUPT_REG);
		pciint = PCI_INTERRUPT_PIN_EXTRACT(data);

		if (pciint) {

			printf (" int %c", 0x60+pciint);

			/*
			**	If the interrupt line register is not set,
			**	set it now from PCI_IRQ.
			*/

			if (!(PCI_INTERRUPT_LINE_EXTRACT(data))) {

				irq = pci_irq & 0x0f;
				pci_irq >>= 4;

				data = PCI_INTERRUPT_LINE_INSERT(data, irq);
				printf (" (config)");
				pcibus.pb_write (tag, PCI_INTERRUPT_REG, data);
			};

			irq = PCI_INTERRUPT_LINE_EXTRACT(data);

			/*
			**	If it's zero, the isa irq number is unknown,
			**	and we cannot bind the pci interrupt to isa.
			*/

			if (irq)
				printf (" irq %d", irq);
			else
				printf (" not bound");
		};

		/*
		**	enable memory access
		*/

		data = (pcibus.pb_read (tag, PCI_COMMAND_STATUS_REG)
			& 0xffff) | PCI_COMMAND_MEM_ENABLE;

		pcibus.pb_write (tag, (u_char) PCI_COMMAND_STATUS_REG, data);

		/*
		**	show pci slot.
		*/

		printf (" on pci%d:%d\n", bus, device);

#ifdef __FreeBSD2__

		/*
		**	Allocate a devconf structure
		*/

		pdcp = (struct pci_devconf *)
			malloc (sizeof (struct pci_devconf),M_DEVBUF,M_WAITOK);

		/*
		**	Fill in.
		**
		**	Sorry, this is not yet complete.
		**	We should, and eventually will, set the
		**	parent pointer to a pci bus devconf structure,
		**	and arrange to set the state field dynamically.
		**
		**	But I'll go to vacation today, and after all,
		**	wasn't there a new feature freeze on Oct 1.?
		*/

		pdcp -> pdc_pi.pi_bus	 = bus;
		pdcp -> pdc_pi.pi_device = device;

		pdcp -> pdc_kdc.kdc_name = dvp->pd_name;
		pdcp -> pdc_kdc.kdc_unit = unit;

		pdcp -> pdc_kdc.kdc_md.mddc_devtype = MDDT_PCI;

		pdcp -> pdc_kdc.kdc_externalize = pci_externalize;
		pdcp -> pdc_kdc.kdc_internalize = pci_internalize;

		pdcp -> pdc_kdc.kdc_datalen     = PCI_EXTERNAL_LEN;
		pdcp -> pdc_kdc.kdc_parentdata  = &pdcp->pdc_pi;
		pdcp -> pdc_kdc.kdc_state       = DC_UNKNOWN;
		pdcp -> pdc_kdc.kdc_description = name;

		/*
		**	And register this device
		*/

		dev_attach (&pdcp->pdc_kdc);

#endif /* __FreeBSD2__ */


		/*
		**	attach device
		**	may produce additional log messages,
		**	i.e. when installing subdevices.
		*/

		(*dvp->pd_attach) (tag, unit);
	    };
	};

#ifndef PCI_QUIET
	if (pci_paddr != pci_pold)
		printf ("pci uses physical addresses from 0x%lx to 0x%lx\n",
			(u_long)pci_pold, (u_long)pci_paddr);
	if (pci_ioaddr != old_ioaddr)
		printf ("pci devices use ioports from 0x%x to 0x%x\n",
			(unsigned)PCI_PORT_START, (unsigned)pci_ioaddr);
#endif
	pci_conf_count++;
}

/*-----------------------------------------------------------------
**
**	The following functions are provided for the device driver
**	to read/write the configuration space.
**
**	pci_conf_read():
**		Read a long word from the pci configuration space.
**		Requires a tag (from pcitag) and the register
**		number (should be a long word alligned one).
**
**	pci_conf_write():
**		Writes a long word to the pci configuration space.
**		Requires a tag (from pcitag), the register number
**		(should be a long word alligned one), and a value.
**
**-----------------------------------------------------------------
*/

u_long
pci_conf_read  (pcici_t tag, u_long reg)
{
	return (pcibus.pb_read (tag, reg));
}

void
pci_conf_write (pcici_t tag, u_long reg, u_long data)
{
	pcibus.pb_write (tag, reg, data);
}

/*-----------------------------------------------------------------------
**
**	Map device into port space.
**
**	PCI-Specification:  6.2.5.1: address maps
**
**-----------------------------------------------------------------------
*/

int pci_map_port (pcici_t tag, u_long reg, u_short* pa)
{
	u_long	data,oldmap;
	u_short	size, ioaddr;

	/*
	**	sanity check
	*/

	if (reg < PCI_MAP_REG_START || reg >= PCI_MAP_REG_END || (reg & 3)) {
		printf ("pci_map_port failed: bad register=0x%x\n",
	       		(unsigned)reg);
		return (0);
	};

	/*
	**	get size and type of port
	**
	**	type is in the lowest two bits.
	**	If device requires 2^n bytes, the next
	**	n-2 bits are hardwired as 0.
	*/

#ifdef PCI_REMAP
	oldmap = 0;
#else
	oldmap = pcibus.pb_read (tag, reg) & 0xfffffffc;
	if (oldmap==0xfffffffc) oldmap=0;
#endif
	pcibus.pb_write (tag, reg, 0xfffffffful);
	data = pcibus.pb_read (tag, reg);

	switch (data & 0x03) {

	case PCI_MAP_IO:
		break;

	default:	/* unknown */
		printf ("pci_map_port failed: bad port type=0x%x\n",
	       		(unsigned) data);
		return (0);
	};

	/*
	**	get the size
	*/

	size = -(data &  PCI_MAP_IO_ADDRESS_MASK);

	if (!size) return (0);

	/*
	**	align physical address to virtual size,
	**	set ioaddr,
	**	and don't forget to increment pci_ioaddr
	*/

	if (oldmap) {
		ioaddr = oldmap;
	} else {
		if ((data = pci_ioaddr % size))
			pci_ioaddr += size - data;
		ioaddr = pci_ioaddr;
		pci_ioaddr += size;
	};

#ifndef PCI_QUIET
	/*
	**	display values.
	*/

	printf ("\treg%d: ioaddr=0x%x size=0x%x\n",
		(unsigned) reg, (unsigned) ioaddr, (unsigned) size);
#endif

	/*
	**	set device address
	*/

	pcibus.pb_write (tag, reg, (u_long) ioaddr);

	/*
	**	return them to the driver
	*/

	*pa = pci_ioaddr;

	return (1);
}

/*-----------------------------------------------------------------------
**
**	Map device into virtual and physical space
**
**	PCI-Specification:  6.2.5.1: address maps
**
**-----------------------------------------------------------------------
*/

int pci_map_mem (pcici_t tag, u_long reg, vm_offset_t* va, vm_offset_t* pa)
{
	u_long data,oldmap,paddr;
	vm_size_t vsize;
	vm_offset_t vaddr;
	int i;

	/*
	**	sanity check
	*/

	if (reg < PCI_MAP_REG_START || reg >= PCI_MAP_REG_END || (reg & 3)) {
		printf ("pci_map_mem failed: bad register=0x%x\n",
	       		(unsigned)reg);
		return (0);
	};

	/*
	**	save old mapping, get size and type of memory
	**
	**	type is in the lowest four bits.
	**	If device requires 2^n bytes, the next
	**	n-4 bits are read as 0.
	*/

#ifdef PCI_REMAP
	oldmap = 0;
#else
	oldmap = pcibus.pb_read (tag, reg) & 0xfffffff0;
	if (oldmap==0xfffffff0) oldmap = 0;
#endif
	pcibus.pb_write (tag, reg, 0xfffffffful);
	data = pcibus.pb_read (tag, reg);

	switch (data & 0x0f) {

	case PCI_MAP_MEMORY_TYPE_32BIT:	/* 32 bit non cachable */
		break;

	default:	/* unknown */
		printf ("pci_map_mem failed: bad memory type=0x%x\n",
	       		(unsigned) data);
		return (0);
	};

	/*
	**	mask out the type,
	**	and round up to a page size
	*/

	vsize = round_page (-(data &  PCI_MAP_MEMORY_ADDRESS_MASK));

	if (!vsize) return (0);

	if (oldmap) {
		paddr = oldmap;
		goto domap;
	};

next_try:
	if (!pci_paddr) {
		/*
		**	Get a starting address.
		*/
		if (pci_pidx >= sizeof(pci_stable)/sizeof(u_long)) {
			printf ("pci_map_mem: out of start addresses.\n");
			return (0);
		};

		pci_paddr = pci_stable[pci_pidx++];
		pci_pold  = 0;

		if (pci_pidx>1)
			printf ("\t(retry at 0x%x)\n",
				(unsigned) pci_paddr);
	};
	/*
	**	align physical address to virtual size
	*/

	if ((data = pci_paddr % vsize))
		pci_paddr += vsize - data;

	if (!pci_pold)
		pci_pold = pci_paddr;

	/*
	**	set physical mapping address,
	**	and reserve physical address range
	*/

	paddr     = pci_paddr;
	pci_paddr += vsize;

domap:
	vaddr = (vm_offset_t) pmap_mapdev (paddr, vsize);

	if (!vaddr) return (0);

#ifndef PCI_QUIET
	/*
	**	display values.
	*/

	printf ("\treg%d: virtual=0x%lx physical=0x%lx\n",
		(unsigned) reg, (u_long)vaddr, (u_long)paddr);
#endif

	/*
	**	probe for already mapped device.
	*/

	if (!oldmap) for (i=0; i<vsize; i+=4) {
		u_long* addr = (u_long*) (vaddr+i);
		data = *addr;
		if (data != 0xffffffff) {
			printf ("\t(possible address conflict: "
				"at 0x%x read: 0x%x)\n",
				(unsigned) paddr+i, (unsigned) data);
			pci_paddr = 0;
			goto next_try;
		};
	};

	/*
	**	Set device address
	*/

	pcibus.pb_write (tag, reg, paddr);

	/*
	**	Check if correctly mapped.
	**
	**	W A R N I N G
	**
	**	This code assumes that the device will NOT return
	**	only ones (0xffffffff) from all offsets.
	*/

	for (i=0; i<vsize; i+=4) {
		u_long* addr = (u_long*) (vaddr+i);
		data = *addr;
		if (data != 0xffffffff)
			break;
	};

	if ((data==0xffffffff) && !oldmap) {
		printf ("\t(possible mapping problem: "
			"at 0x%x read 0xffffffff)\n",
			(unsigned) paddr);
		pci_paddr = 0;
		goto next_try;
	};

	/*
	**	Return addresses to the driver
	*/

	*va = vaddr;
	*pa = paddr;

	return (1);
}

/*-----------------------------------------------------------------------
**
**	Map new pci bus. (XXX under construction)
**
**	PCI-Specification: ____________?
**
**-----------------------------------------------------------------------
*/

int pci_map_bus (pcici_t tag, u_long bus)
{
	if (bus >= PCI_MAX_BUS) {
		printf ("pci_map_bus failed: bus number %d too big.\n",
			(int) bus);
		return (0);
	};

	if (bus >= pci_bus_max)
		pci_bus_max = bus + 1;

#ifndef PCI_QUIET
	/*
	**	display values.
	*/

	printf ("\tmapped pci bus %d.\n",
		(int) bus);
#endif

	return (1);
}

/*------------------------------------------------------------
**
**	Interface functions for the devconf module.
**
**------------------------------------------------------------
*/

static int
pci_externalize (struct proc *p, struct kern_devconf *kdcp, void *u, size_t l)
{
	struct pci_externalize_buffer buffer;
	struct pci_info * pip = kdcp->kdc_parentdata;
	pcici_t tag;
	int	i;

	if (l < sizeof buffer) {
		return ENOMEM;
	};

	tag = pcibus.pb_tag (pip->pi_bus, pip->pi_device, 0);

	buffer.peb_pci_info	= *pip;

	for (i=0; i<PCI_EXT_CONF_LEN; i++) {
		buffer.peb_config[i] = pcibus.pb_read (tag, i*4);
	};

	return copyout(&buffer, u, sizeof buffer);
}
 
 
static int
pci_internalize (struct proc *p, struct kern_devconf *kdcp, void *u, size_t s)
{
	return EOPNOTSUPP;
}

/*-----------------------------------------------------------------------
**
**	Map pci interrupts to isa interrupts.
**
**-----------------------------------------------------------------------
*/

int pci_map_int (pcici_t tag, int(*func)(), void* arg, unsigned* maskptr)
{
	int irq, result;

	irq = PCI_INTERRUPT_LINE_EXTRACT(
		pcibus.pb_read (tag, PCI_INTERRUPT_REG));

	if (irq >= 16 || irq <= 0) {
		printf ("pci_map_int failed: no int line set.\n");
		return (0);
	}

	result = pcibus.pb_regint (tag, func, arg, maskptr);

	if (!result) {
		printf ("pci_map_int failed.\n");
		return (0);
	};

	return (1);
}

/*-----------------------------------------------------------
**
**	Display of unknown devices.
**
**-----------------------------------------------------------
*/
struct vt {
	u_short	ident;
	char*	name;
};

static struct vt VendorTable[] = {
	{0x1002, "ATI TECHNOLOGIES INC"},
	{0x1011, "DIGITAL EQUIPMENT CORPORATION"},
	{0x101A, "NCR"},
	{0x102B, "MATROX"},
	{0x1045, "OPTI"},
	{0x5333, "S3 INC."},
	{0x8086, "INTEL CORPORATION"},
	{0,0}
};

static const char *const majclasses[] = {
	"old", "storage", "network", "display",
	"multimedia", "memory", "bridge" 
};

void not_supported (pcici_t tag, u_long type)
{
	u_char	reg;
	u_long	data;
	struct vt * vp;

	/*
	**	lookup the names.
	*/

	for (vp=VendorTable; vp->ident; vp++)
		if (vp->ident == (type & 0xffff))
			break;

	/*
	**	and display them.
	*/

	if (vp->ident) printf (vp->name);
		else   printf ("vendor=0x%lx", type & 0xffff);

	printf (", device=0x%lx", type >> 16);

	data = (pcibus.pb_read(tag, PCI_CLASS_REG) >> 24) & 0xff;
	if (data < sizeof(majclasses) / sizeof(majclasses[0]))
		printf(", class=%s", majclasses[data]);

	printf (" [not supported]\n");

	for (reg=PCI_MAP_REG_START; reg<PCI_MAP_REG_END; reg+=4) {
		data = pcibus.pb_read (tag, reg);
		if (!data) continue;
		switch (data&7) {

		case 1:
		case 5:
			printf ("	map(%x): io(%lx)\n",
				reg, data & ~3);
			break;
		case 0:
			printf ("	map(%x): mem32(%lx)\n",
				reg, data & ~7);
			break;
		case 2:
			printf ("	map(%x): mem20(%lx)\n",
				reg, data & ~7);
			break;
		case 4:
			printf ("	map(%x): mem64(%lx)\n",
				reg, data & ~7);
			break;
		}
	}
}

#ifndef __FreeBSD2__
/*-----------------------------------------------------------
**
**	Mapping of physical to virtual memory
**
**-----------------------------------------------------------
*/

extern vm_map_t kernel_map;

static	vm_offset_t pmap_mapdev (vm_offset_t paddr, vm_size_t vsize)
{
	vm_offset_t	vaddr,value;
	u_long		result;

	vaddr  = vm_map_min (kernel_map);

	result = vm_map_find (kernel_map, (void*)0, (vm_offset_t) 0,
				&vaddr, vsize, TRUE);

	if (result != KERN_SUCCESS) {
		printf (" vm_map_find failed(%d)\n", result);
		return (0);
	};

	/*
	**	map physical
	*/

	value = vaddr;
	while (vsize >= NBPG) {
		pmap_enter (pmap_kernel(), vaddr, paddr,
				VM_PROT_READ|VM_PROT_WRITE, TRUE);
		vaddr += NBPG;
		paddr += NBPG;
		vsize -= NBPG;
	};
	return (value);
}

/*------------------------------------------------------------
**
**	Emulate the register_intr() function of FreeBSD 2.0
**
**	requires a patch:
**	FreeBSD 2.0:	"/sys/i386/isa/vector.s"
**	386bsd0.1:	"/sys/i386/isa/icu.s"
**	386bsd1.0:	Please ask Jesus Monroy Jr.
**
**------------------------------------------------------------
*/

#include <machine/segments.h>

int		pci_int_unit [16];
inthand2_t*    (pci_int_hdlr [16]);
unsigned int *	pci_int_mptr [16];
unsigned int	pci_int_count[16];

extern void
	Vpci3(), Vpci4(), Vpci5(), Vpci6(), Vpci7(), Vpci8(), Vpci9(),
	Vpci10(), Vpci11(), Vpci12(), Vpci13(), Vpci14(), Vpci15();

static inthand_t* pci_int_glue[16] = {
	0, 0, 0, Vpci3, Vpci4, Vpci5, Vpci6, Vpci7, Vpci8,
	Vpci9, Vpci10, Vpci11, Vpci12, Vpci13, Vpci14, Vpci15 };

static int
register_intr __P((int intr, int device_id, unsigned int flags,
		       inthand2_t *handler, unsigned int* mptr, int unit))
{
	if (intr >= 16 || intr <= 2)
		return (EINVAL);
	if (pci_int_hdlr [intr])
		return (EBUSY);

	pci_int_hdlr [intr] = handler;
	pci_int_unit [intr] = unit;
	pci_int_mptr [intr] = mptr;

	setidt(NRSVIDT + intr, pci_int_glue[intr], SDT_SYS386IGT, SEL_KPL);
	return (0);
}
#endif /* __FreeBSD2__ */
#endif /* NPCI */
OpenPOWER on IntegriCloud