xf86pciBus.c revision 52397711
1/*
2 * Copyright (c) 1997-2003 by The XFree86 Project, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Except as contained in this notice, the name of the copyright holder(s)
23 * and author(s) shall not be used in advertising or otherwise to promote
24 * the sale, use or other dealings in this Software without prior written
25 * authorization from the copyright holder(s) and author(s).
26 */
27
28/*
29 * This file contains the interfaces to the bus-specific code
30 */
31#ifdef HAVE_XORG_CONFIG_H
32#include <xorg-config.h>
33#endif
34
35#include <ctype.h>
36#include <stdlib.h>
37#include <unistd.h>
38#include <X11/X.h>
39#include <pciaccess.h>
40#include "os.h"
41#include "Pci.h"
42#include "xf86.h"
43#include "xf86Priv.h"
44#include "xf86Resources.h"
45
46/* Bus-specific headers */
47#include "xf86Bus.h"
48
49#define XF86_OS_PRIVS
50#define NEED_OS_RAC_PROTOS
51#include "xf86_OSproc.h"
52
53#include "xf86RAC.h"
54
55/* Bus-specific globals */
56Bool pciSlotClaimed = FALSE;
57static struct pci_device ** xf86PciVideoInfo = NULL;	/* PCI probe for video hw */
58
59
60/* PCI classes that get included in xf86PciVideoInfo */
61#define PCIINFOCLASSES(c) \
62    ( (((c) & 0x00ff0000) == (PCI_CLASS_PREHISTORIC << 16)) \
63      || (((c) & 0x00ff0000) == (PCI_CLASS_DISPLAY << 16)) \
64      || ((((c) & 0x00ffff00) \
65	   == ((PCI_CLASS_MULTIMEDIA << 16) | (PCI_SUBCLASS_MULTIMEDIA_VIDEO << 8)))) \
66      || ((((c) & 0x00ffff00) \
67	   == ((PCI_CLASS_PROCESSOR << 16) | (PCI_SUBCLASS_PROCESSOR_COPROC << 8)))) )
68
69/*
70 * PCI classes that have messages printed always.  The others are only
71 * have a message printed when the vendor/dev IDs are recognised.
72 */
73#define PCIALWAYSPRINTCLASSES(c) \
74    ( (((c) & 0x00ffff00) \
75       == ((PCI_CLASS_PREHISTORIC << 16) | (PCI_SUBCLASS_PREHISTORIC_VGA << 8))) \
76      || (((c) & 0x00ff0000) == (PCI_CLASS_DISPLAY << 16)) \
77      || ((((c) & 0x00ffff00) \
78	   == ((PCI_CLASS_MULTIMEDIA << 16) | (PCI_SUBCLASS_MULTIMEDIA_VIDEO << 8)))) )
79
80#define IS_VGA(c) \
81    (((c) & 0x00ffff00) \
82	 == ((PCI_CLASS_DISPLAY << 16) | (PCI_SUBCLASS_DISPLAY_VGA << 8)))
83
84/*
85 * PCI classes for which potentially destructive checking of the map sizes
86 * may be done.  Any classes where this may be unsafe should be omitted
87 * from this list.
88 */
89#define PCINONSYSTEMCLASSES(c) PCIALWAYSPRINTCLASSES(c)
90
91/*
92 * PCI classes that use RAC
93 */
94#define PCISHAREDIOCLASSES(c) \
95    ( (((c) & 0x00ffff00) \
96       == ((PCI_CLASS_PREHISTORIC << 16) | (PCI_SUBCLASS_PREHISTORIC_VGA << 8))) \
97      || IS_VGA(c) )
98
99
100_X_EXPORT void
101xf86FormatPciBusNumber(int busnum, char *buffer)
102{
103    /* 'buffer' should be at least 8 characters long */
104    if (busnum < 256)
105	sprintf(buffer, "%d", busnum);
106    else
107	sprintf(buffer, "%d@%d", busnum & 0x00ff, busnum >> 8);
108}
109
110/*
111 * IO enable/disable related routines for PCI
112 */
113#define pArg ((pciArg*)arg)
114#define SETBITS PCI_CMD_IO_ENABLE
115static void
116pciIoAccessEnable(void* arg)
117{
118#if 0
119#ifdef DEBUG
120    ErrorF("pciIoAccessEnable: 0x%05lx\n", *(PCITAG *)arg);
121#endif
122    pArg->ctrl |= SETBITS | PCI_CMD_MASTER_ENABLE;
123    pci_device_cfg_write_u32(pArg->dev, pArg->ctrl, PCI_CMD_STAT_REG);
124#endif
125}
126
127static void
128pciIoAccessDisable(void* arg)
129{
130#if 0
131#ifdef DEBUG
132    ErrorF("pciIoAccessDisable: 0x%05lx\n", *(PCITAG *)arg);
133#endif
134    pArg->ctrl &= ~SETBITS;
135    pci_device_cfg_write_u32(pArg->dev, pArg->ctrl, PCI_CMD_STAT_REG);
136#endif
137}
138
139#undef SETBITS
140#define SETBITS (PCI_CMD_IO_ENABLE | PCI_CMD_MEM_ENABLE)
141static void
142pciIo_MemAccessEnable(void* arg)
143{
144#if 0
145#ifdef DEBUG
146    ErrorF("pciIo_MemAccessEnable: 0x%05lx\n", *(PCITAG *)arg);
147#endif
148    pArg->ctrl |= SETBITS | PCI_CMD_MASTER_ENABLE;
149    pci_device_cfg_write_u32(pArg->dev, pArg->ctrl, PCI_CMD_STAT_REG);
150#endif
151}
152
153static void
154pciIo_MemAccessDisable(void* arg)
155{
156#if 0
157#ifdef DEBUG
158    ErrorF("pciIo_MemAccessDisable: 0x%05lx\n", *(PCITAG *)arg);
159#endif
160    pArg->ctrl &= ~SETBITS;
161    pci_device_cfg_write_u32(pArg->dev, pArg->ctrl, PCI_CMD_STAT_REG);
162#endif
163}
164
165#undef SETBITS
166#define SETBITS (PCI_CMD_MEM_ENABLE)
167static void
168pciMemAccessEnable(void* arg)
169{
170#if 0
171#ifdef DEBUG
172    ErrorF("pciMemAccessEnable: 0x%05lx\n", *(PCITAG *)arg);
173#endif
174    pArg->ctrl |= SETBITS | PCI_CMD_MASTER_ENABLE;
175    pci_device_cfg_write_u32(pArg->dev, pArg->ctrl, PCI_CMD_STAT_REG);
176#endif
177}
178
179static void
180pciMemAccessDisable(void* arg)
181{
182#if 0
183#ifdef DEBUG
184    ErrorF("pciMemAccessDisable: 0x%05lx\n", *(PCITAG *)arg);
185#endif
186    pArg->ctrl &= ~SETBITS;
187    pci_device_cfg_write_u32(pArg->dev, pArg->ctrl, PCI_CMD_STAT_REG);
188#endif
189}
190#undef SETBITS
191#undef pArg
192
193
194/* move to OS layer */
195#define MASKBITS (PCI_PCI_BRIDGE_VGA_EN | PCI_PCI_BRIDGE_MASTER_ABORT_EN)
196static void
197pciBusAccessEnable(BusAccPtr ptr)
198{
199#if 0
200    struct pci_device * const dev = ptr->busdep.pci.dev;
201    uint16_t ctrl;
202
203#ifdef DEBUG
204    ErrorF("pciBusAccessEnable: bus=%d\n", ptr->busdep.pci.bus);
205#endif
206    pci_device_cfg_read_u16( dev, & ctrl, PCI_PCI_BRIDGE_CONTROL_REG );
207    if ((ctrl & MASKBITS) != PCI_PCI_BRIDGE_VGA_EN) {
208	ctrl = (ctrl | PCI_PCI_BRIDGE_VGA_EN) &
209	    ~(PCI_PCI_BRIDGE_MASTER_ABORT_EN | PCI_PCI_BRIDGE_SECONDARY_RESET);
210	pci_device_cfg_write_u16(dev, ctrl, PCI_PCI_BRIDGE_CONTROL_REG);
211    }
212#endif
213}
214
215/* move to OS layer */
216static void
217pciBusAccessDisable(BusAccPtr ptr)
218{
219#if 0
220    struct pci_device * const dev = ptr->busdep.pci.dev;
221    uint16_t ctrl;
222
223#ifdef DEBUG
224    ErrorF("pciBusAccessDisable: bus=%d\n", ptr->busdep.pci.bus);
225#endif
226    pci_device_cfg_read_u16( dev, & ctrl, PCI_PCI_BRIDGE_CONTROL_REG );
227    if (ctrl & MASKBITS) {
228	ctrl &= ~(MASKBITS | PCI_PCI_BRIDGE_SECONDARY_RESET);
229	pci_device_cfg_write_u16(dev, ctrl, PCI_PCI_BRIDGE_CONTROL_REG);
230    }
231#endif
232}
233#undef MASKBITS
234
235static void
236pciSetBusAccess(BusAccPtr ptr)
237{
238#if 0
239#ifdef DEBUG
240    ErrorF("pciSetBusAccess: route VGA to bus %d\n", ptr->busdep.pci.bus);
241#endif
242
243    if (!ptr->primary && !ptr->current)
244	return;
245
246    if (ptr->current && ptr->current->disable_f)
247	(*ptr->current->disable_f)(ptr->current);
248    ptr->current = NULL;
249
250    /* walk down */
251    while (ptr->primary) {	/* No enable for root bus */
252	if (ptr != ptr->primary->current) {
253	    if (ptr->primary->current && ptr->primary->current->disable_f)
254		(*ptr->primary->current->disable_f)(ptr->primary->current);
255	    if (ptr->enable_f)
256		(*ptr->enable_f)(ptr);
257	    ptr->primary->current = ptr;
258	}
259	ptr = ptr->primary;
260    }
261#endif
262}
263
264/* move to OS layer */
265static void
266savePciState( struct pci_device * dev, pciSavePtr ptr )
267{
268#if 0
269    int i;
270
271    pci_device_cfg_read_u32( dev, & ptr->command, PCI_CMD_STAT_REG );
272
273    for ( i = 0; i < 6; i++ ) {
274	pci_device_cfg_read_u32( dev, & ptr->base[i],
275				 PCI_CMD_BASE_REG + (i * 4) );
276    }
277
278    pci_device_cfg_read_u32( dev, & ptr->biosBase, PCI_CMD_BIOS_REG );
279#endif
280}
281
282/* move to OS layer */
283#if 0
284static void
285restorePciState( struct pci_device * dev, pciSavePtr ptr)
286{
287    int i;
288
289    /* disable card before setting anything */
290    pci_device_cfg_write_bits(dev, PCI_CMD_MEM_ENABLE | PCI_CMD_IO_ENABLE, 0,
291			      PCI_CMD_STAT_REG);
292
293    pci_device_cfg_write_u32(dev, ptr->biosBase, PCI_CMD_BIOS_REG);
294
295    for ( i = 0; i < 6; i++ ) {
296	pci_device_cfg_write_u32(dev, ptr->base[i],
297				 PCI_CMD_BASE_REG + (i * 4));
298    }
299
300    pci_device_cfg_write_u32(dev, ptr->command, PCI_CMD_STAT_REG);
301}
302#endif
303
304/* move to OS layer */
305static void
306savePciBusState(BusAccPtr ptr)
307{
308#if 0
309    struct pci_device * const dev = ptr->busdep.pci.dev;
310    uint16_t temp;
311
312    pci_device_cfg_read_u16( dev, & temp, PCI_PCI_BRIDGE_CONTROL_REG );
313    ptr->busdep.pci.save.control = temp & ~PCI_PCI_BRIDGE_SECONDARY_RESET;
314
315    /* Allow master aborts to complete normally on non-root buses */
316    if ( ptr->busdep.pci.save.control & PCI_PCI_BRIDGE_MASTER_ABORT_EN ) {
317	temp = ptr->busdep.pci.save.control & ~PCI_PCI_BRIDGE_MASTER_ABORT_EN;
318	pci_device_cfg_read_u16( dev, & temp, PCI_PCI_BRIDGE_CONTROL_REG );
319    }
320#endif
321}
322
323/* move to OS layer */
324#define MASKBITS (PCI_PCI_BRIDGE_VGA_EN | PCI_PCI_BRIDGE_MASTER_ABORT_EN)
325static void
326restorePciBusState(BusAccPtr ptr)
327{
328#if 0
329    struct pci_device * const dev = ptr->busdep.pci.dev;
330    uint16_t ctrl;
331
332    /* Only restore the bits we've changed (and don't cause resets) */
333    pci_device_cfg_read_u16( dev, & ctrl, PCI_PCI_BRIDGE_CONTROL_REG );
334
335    if ((ctrl ^ ptr->busdep.pci.save.control) & MASKBITS) {
336	ctrl &= ~(MASKBITS | PCI_PCI_BRIDGE_SECONDARY_RESET);
337	ctrl |= ptr->busdep.pci.save.control & MASKBITS;
338	pci_device_cfg_write_u16(dev, ctrl, PCI_PCI_BRIDGE_CONTROL_REG);
339    }
340#endif
341}
342#undef MASKBITS
343
344
345/*
346 * xf86Bus.c interface
347 */
348
349void
350xf86PciProbe(void)
351{
352    int i = 0, k;
353    int num = 0;
354    struct pci_device *info;
355    struct pci_device_iterator *iter;
356
357
358    if (!xf86scanpci()) {
359	xf86PciVideoInfo = NULL;
360	return;
361    }
362
363    iter = pci_slot_match_iterator_create(& xf86IsolateDevice);
364    while ((info = pci_device_next(iter)) != NULL) {
365	if (PCIINFOCLASSES(info->device_class)) {
366	    num++;
367	    xf86PciVideoInfo = xnfrealloc(xf86PciVideoInfo,
368					  (sizeof(struct pci_device *)
369					   * (num + 1)));
370	    xf86PciVideoInfo[num] = NULL;
371	    xf86PciVideoInfo[num - 1] = info;
372
373	    pci_device_probe(info);
374	    info->user_data = 0;
375	}
376    }
377
378
379    /* If we haven't found a primary device try a different heuristic */
380    if (primaryBus.type == BUS_NONE && num) {
381	for (i = 0; i < num; i++) {
382	    uint16_t  command;
383
384	    info = xf86PciVideoInfo[i];
385	    pci_device_cfg_read_u16(info, & command, 4);
386
387	    if ((command & PCI_CMD_MEM_ENABLE)
388		&& ((num == 1) || IS_VGA(info->device_class))) {
389		if (primaryBus.type == BUS_NONE) {
390		    primaryBus.type = BUS_PCI;
391		    primaryBus.id.pci = info;
392		} else {
393		    xf86Msg(X_NOTICE,
394			    "More than one possible primary device found\n");
395		    primaryBus.type ^= (BusType)(-1);
396		}
397	    }
398	}
399    }
400
401    /* Print a summary of the video devices found */
402    for (k = 0; k < num; k++) {
403	const char *vendorname = NULL, *chipname = NULL;
404	const char *prim = " ";
405	Bool memdone = FALSE, iodone = FALSE;
406
407
408	info = xf86PciVideoInfo[k];
409
410	vendorname = pci_device_get_vendor_name( info );
411	chipname = pci_device_get_device_name( info );
412
413	if ((!vendorname || !chipname) &&
414	    !PCIALWAYSPRINTCLASSES(info->device_class))
415	    continue;
416
417	if (xf86IsPrimaryPci(info))
418	    prim = "*";
419
420	xf86Msg(X_PROBED, "PCI:%s(%u:%u:%u:%u) %04x:%04x:%04x:%04x ", prim,
421		info->domain, info->bus, info->dev, info->func,
422		info->vendor_id, info->device_id,
423		info->subvendor_id, info->subdevice_id);
424
425	if (vendorname)
426	    xf86ErrorF("%s ", vendorname);
427
428	if (chipname)
429	    xf86ErrorF("%s ", chipname);
430
431	xf86ErrorF("rev %d", info->revision);
432
433	for (i = 0; i < 6; i++) {
434	    struct pci_mem_region * r = & info->regions[i];
435
436	    if ( r->size && ! r->is_IO ) {
437		if (!memdone) {
438		    xf86ErrorF(", Mem @ ");
439		    memdone = TRUE;
440		} else
441		    xf86ErrorF(", ");
442		xf86ErrorF("0x%08lx/%ld", (long)r->base_addr, (long)r->size);
443	    }
444	}
445
446	for (i = 0; i < 6; i++) {
447	    struct pci_mem_region * r = & info->regions[i];
448
449	    if ( r->size && r->is_IO ) {
450		if (!iodone) {
451		    xf86ErrorF(", I/O @ ");
452		    iodone = TRUE;
453		} else
454		    xf86ErrorF(", ");
455		xf86ErrorF("0x%08lx/%ld", (long)r->base_addr, (long)r->size);
456	    }
457	}
458
459	if ( info->rom_size ) {
460	    xf86ErrorF(", BIOS @ 0x\?\?\?\?\?\?\?\?/%ld", (long)info->rom_size);
461	}
462
463	xf86ErrorF("\n");
464    }
465}
466
467void
468initPciState(void)
469{
470    unsigned i;
471    pciAccPtr pcaccp;
472
473    if (xf86PciVideoInfo == NULL) {
474	return;
475    }
476
477    for (i = 0 ; xf86PciVideoInfo[i] != NULL ; i++) {
478	struct pci_device * const pvp = xf86PciVideoInfo[i];
479
480	if (pvp->user_data == 0) {
481	    pcaccp = xnfalloc( sizeof( pciAccRec ) );
482	    pvp->user_data = (intptr_t) pcaccp;
483
484	    pcaccp->arg.dev = pvp;
485	    pcaccp->ioAccess.AccessDisable = pciIoAccessDisable;
486	    pcaccp->ioAccess.AccessEnable = pciIoAccessEnable;
487	    pcaccp->ioAccess.arg = &pcaccp->arg;
488	    pcaccp->io_memAccess.AccessDisable = pciIo_MemAccessDisable;
489	    pcaccp->io_memAccess.AccessEnable = pciIo_MemAccessEnable;
490	    pcaccp->io_memAccess.arg = &pcaccp->arg;
491	    pcaccp->memAccess.AccessDisable = pciMemAccessDisable;
492	    pcaccp->memAccess.AccessEnable = pciMemAccessEnable;
493	    pcaccp->memAccess.arg = &pcaccp->arg;
494
495	    pcaccp->ctrl = PCISHAREDIOCLASSES(pvp->device_class);
496
497	    savePciState(pvp, &pcaccp->save);
498	    pcaccp->arg.ctrl = pcaccp->save.command;
499	}
500    }
501}
502
503/*
504 * initPciBusState() - fill out the BusAccRec for a PCI bus.
505 * Theory: each bus is associated with one bridge connecting it
506 * to its parent bus. The address of a bridge is therefore stored
507 * in the BusAccRec of the bus it connects to. Each bus can
508 * have several bridges connecting secondary buses to it. Only one
509 * of these bridges can be open. Therefore the status of a bridge
510 * associated with a bus is stored in the BusAccRec of the parent
511 * the bridge connects to. The first member of the structure is
512 * a pointer to a function that open access to this bus. This function
513 * receives a pointer to the structure itself as argument. This
514 * design should be common to BusAccRecs of any type of buses we
515 * support. The remeinder of the structure is bus type specific.
516 * In this case it contains a pointer to the structure of the
517 * parent bus. Thus enabling access to a specific bus is simple:
518 * 1. Close any bridge going to secondary buses.
519 * 2. Climb down the ladder and enable any bridge on buses
520 *    on the path from the CPU to this bus.
521 */
522
523void
524initPciBusState(void)
525{
526    static const struct pci_id_match bridge_match = {
527	PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY,
528	(PCI_CLASS_BRIDGE << 16), 0x0000ff0000, 0
529    };
530    struct pci_device *dev;
531    struct pci_device_iterator *iter;
532    BusAccPtr pbap, pbap_tmp;
533
534    iter = pci_id_match_iterator_create(& bridge_match);
535    while((dev = pci_device_next(iter)) != NULL) {
536	const uint8_t subclass = (dev->device_class >> 8) & 0x0ff;
537	int primary;
538	int secondary;
539	int subordinate;
540
541
542	pci_device_get_bridge_buses(dev, &primary, &secondary, &subordinate);
543
544	pbap = xnfcalloc(1,sizeof(BusAccRec));
545	pbap->busdep.pci.bus = secondary;
546	pbap->busdep.pci.primary_bus = primary;
547	pbap->busdep_type = BUS_PCI;
548	pbap->busdep.pci.dev = dev;
549
550	pbap->set_f = pciSetBusAccess;
551
552	switch (subclass) {
553	case PCI_SUBCLASS_BRIDGE_HOST:
554	    pbap->type = BUS_PCI;
555	    break;
556	case PCI_SUBCLASS_BRIDGE_PCI:
557	case PCI_SUBCLASS_BRIDGE_CARDBUS:
558	    pbap->type = BUS_PCI;
559	    pbap->save_f = savePciBusState;
560	    pbap->restore_f = restorePciBusState;
561	    pbap->enable_f = pciBusAccessEnable;
562	    pbap->disable_f = pciBusAccessDisable;
563	    savePciBusState(pbap);
564	    break;
565	}
566	pbap->next = xf86BusAccInfo;
567	xf86BusAccInfo = pbap;
568    }
569
570    pci_iterator_destroy(iter);
571
572    for (pbap = xf86BusAccInfo; pbap; pbap = pbap->next) {
573	pbap->primary = NULL;
574
575	if (pbap->busdep_type == BUS_PCI
576	    && pbap->busdep.pci.primary_bus > -1) {
577	    pbap_tmp = xf86BusAccInfo;
578	    while (pbap_tmp) {
579		if (pbap_tmp->busdep_type == BUS_PCI &&
580		    pbap_tmp->busdep.pci.bus == pbap->busdep.pci.primary_bus) {
581		    /* Don't create loops */
582		    if (pbap == pbap_tmp)
583			break;
584		    pbap->primary = pbap_tmp;
585		    break;
586		}
587		pbap_tmp = pbap_tmp->next;
588	    }
589	}
590    }
591}
592
593void
594PciStateEnter(void)
595{
596#if 0
597    unsigned i;
598
599    if (xf86PciVideoInfo == NULL)
600	return;
601
602    for ( i = 0 ; xf86PciVideoInfo[i] != NULL ; i++ ) {
603	pciAccPtr paccp = (pciAccPtr) xf86PciVideoInfo[i]->user_data;
604
605 	if ( (paccp != NULL) && paccp->ctrl ) {
606	    savePciState(paccp->arg.dev, &paccp->save);
607	    restorePciState(paccp->arg.dev, &paccp->restore);
608	    paccp->arg.ctrl = paccp->restore.command;
609	}
610    }
611#endif
612}
613
614void
615PciBusStateEnter(void)
616{
617#if 0
618    BusAccPtr pbap = xf86BusAccInfo;
619
620    while (pbap) {
621	if (pbap->save_f)
622	    pbap->save_f(pbap);
623	pbap = pbap->next;
624    }
625#endif
626}
627
628void
629PciStateLeave(void)
630{
631#if 0
632    unsigned i;
633
634    if (xf86PciVideoInfo == NULL)
635	return;
636
637    for ( i = 0 ; xf86PciVideoInfo[i] != NULL ; i++ ) {
638	pciAccPtr paccp = (pciAccPtr) xf86PciVideoInfo[i]->user_data;
639
640 	if ( (paccp != NULL) && paccp->ctrl ) {
641	    savePciState(paccp->arg.dev, &paccp->restore);
642	    restorePciState(paccp->arg.dev, &paccp->save);
643	}
644    }
645#endif
646}
647
648void
649PciBusStateLeave(void)
650{
651#if 0
652    BusAccPtr pbap = xf86BusAccInfo;
653
654    while (pbap) {
655	if (pbap->restore_f)
656	    pbap->restore_f(pbap);
657	pbap = pbap->next;
658    }
659#endif
660}
661
662void
663DisablePciAccess(void)
664{
665#if 0
666    unsigned i;
667
668    if (xf86PciVideoInfo == NULL)
669	return;
670
671    for ( i = 0 ; xf86PciVideoInfo[i] != NULL ; i++ ) {
672	pciAccPtr paccp = (pciAccPtr) xf86PciVideoInfo[i]->user_data;
673
674 	if ( (paccp != NULL) && paccp->ctrl ) {
675	    pciIo_MemAccessDisable(paccp->io_memAccess.arg);
676	}
677    }
678#endif
679}
680
681void
682DisablePciBusAccess(void)
683{
684#if 0
685    BusAccPtr pbap = xf86BusAccInfo;
686
687    while (pbap) {
688	if (pbap->disable_f)
689	    pbap->disable_f(pbap);
690	if (pbap->primary)
691	    pbap->primary->current = NULL;
692	pbap = pbap->next;
693    }
694#endif
695}
696
697/*
698 * If the slot requested is already in use, return -1.
699 * Otherwise, claim the slot for the screen requesting it.
700 */
701
702_X_EXPORT int
703xf86ClaimPciSlot(struct pci_device * d, DriverPtr drvp,
704		 int chipset, GDevPtr dev, Bool active)
705{
706    EntityPtr p = NULL;
707    pciAccPtr paccp = (pciAccPtr) d->user_data;
708    BusAccPtr pbap = xf86BusAccInfo;
709    const unsigned bus = PCI_MAKE_BUS(d->domain, d->bus);
710
711    int num;
712
713    if (xf86CheckPciSlot(d)) {
714	num = xf86AllocateEntity();
715	p = xf86Entities[num];
716	p->driver = drvp;
717	p->chipset = chipset;
718	p->bus.type = BUS_PCI;
719	p->bus.id.pci = d;
720	p->active = active;
721	p->inUse = FALSE;
722	if (dev)
723            xf86AddDevToEntity(num, dev);
724	/* Here we initialize the access structure */
725	p->access = xnfcalloc(1,sizeof(EntityAccessRec));
726	if (paccp != NULL) {
727	    p->access->fallback = & paccp->io_memAccess;
728	    p->access->pAccess = & paccp->io_memAccess;
729	    paccp->ctrl = TRUE; /* mark control if not already */
730	}
731	else {
732	    p->access->fallback = &AccessNULL;
733	    p->access->pAccess = &AccessNULL;
734	}
735
736	p->busAcc = NULL;
737	while (pbap) {
738	    if (pbap->type == BUS_PCI && pbap->busdep.pci.bus == bus)
739		p->busAcc = pbap;
740	    pbap = pbap->next;
741	}
742
743	pciSlotClaimed = TRUE;
744
745	if (active) {
746	    /* Map in this domain's I/O space */
747	   p->domainIO = xf86MapLegacyIO(d);
748	}
749
750 	return num;
751    } else
752 	return -1;
753}
754
755/*
756 * Parse a BUS ID string, and return the PCI bus parameters if it was
757 * in the correct format for a PCI bus id.
758 */
759
760_X_EXPORT Bool
761xf86ParsePciBusString(const char *busID, int *bus, int *device, int *func)
762{
763    /*
764     * The format is assumed to be "bus[@domain]:device[:func]", where domain,
765     * bus, device and func are decimal integers.  domain and func may be
766     * omitted and assumed to be zero, although doing this isn't encouraged.
767     */
768
769    char *p, *s, *d;
770    const char *id;
771    int i;
772
773    if (StringToBusType(busID, &id) != BUS_PCI)
774	return FALSE;
775
776    s = xstrdup(id);
777    p = strtok(s, ":");
778    if (p == NULL || *p == 0) {
779	xfree(s);
780	return FALSE;
781    }
782    d = strpbrk(p, "@");
783    if (d != NULL) {
784	*(d++) = 0;
785	for (i = 0; d[i] != 0; i++) {
786	    if (!isdigit(d[i])) {
787		xfree(s);
788		return FALSE;
789	    }
790	}
791    }
792    for (i = 0; p[i] != 0; i++) {
793	if (!isdigit(p[i])) {
794	    xfree(s);
795	    return FALSE;
796	}
797    }
798    *bus = atoi(p);
799    if (d != NULL && *d != 0)
800	*bus += atoi(d) << 8;
801    p = strtok(NULL, ":");
802    if (p == NULL || *p == 0) {
803	xfree(s);
804	return FALSE;
805    }
806    for (i = 0; p[i] != 0; i++) {
807	if (!isdigit(p[i])) {
808	    xfree(s);
809	    return FALSE;
810	}
811    }
812    *device = atoi(p);
813    *func = 0;
814    p = strtok(NULL, ":");
815    if (p == NULL || *p == 0) {
816	xfree(s);
817	return TRUE;
818    }
819    for (i = 0; p[i] != 0; i++) {
820	if (!isdigit(p[i])) {
821	    xfree(s);
822	    return FALSE;
823	}
824    }
825    *func = atoi(p);
826    xfree(s);
827    return TRUE;
828}
829
830/*
831 * Compare a BUS ID string with a PCI bus id.  Return TRUE if they match.
832 */
833
834_X_EXPORT Bool
835xf86ComparePciBusString(const char *busID, int bus, int device, int func)
836{
837    int ibus, idevice, ifunc;
838
839    if (xf86ParsePciBusString(busID, &ibus, &idevice, &ifunc)) {
840	return bus == ibus && device == idevice && func == ifunc;
841    } else {
842	return FALSE;
843    }
844}
845
846/*
847 * xf86IsPrimaryPci() -- return TRUE if primary device
848 * is PCI and bus, dev and func numbers match.
849 */
850
851_X_EXPORT Bool
852xf86IsPrimaryPci(struct pci_device *pPci)
853{
854    return ((primaryBus.type == BUS_PCI) && (pPci == primaryBus.id.pci));
855}
856
857/*
858 * xf86GetPciInfoForEntity() -- Get the pciVideoRec of entity.
859 */
860_X_EXPORT struct pci_device *
861xf86GetPciInfoForEntity(int entityIndex)
862{
863    EntityPtr p;
864
865    if (entityIndex >= xf86NumEntities)
866	return NULL;
867
868    p = xf86Entities[entityIndex];
869    return (p->bus.type == BUS_PCI) ? p->bus.id.pci : NULL;
870}
871
872/*
873 * xf86CheckPciMemBase() checks that the memory base value matches one of the
874 * PCI base address register values for the given PCI device.
875 */
876_X_EXPORT Bool
877xf86CheckPciMemBase( struct pci_device * pPci, memType base )
878{
879    int i;
880
881    for (i = 0; i < 6; i++)
882	if (base == pPci->regions[i].base_addr)
883	    return TRUE;
884    return FALSE;
885}
886
887/*
888 * Check if the slot requested is free.  If it is already in use, return FALSE.
889 */
890
891_X_EXPORT Bool
892xf86CheckPciSlot(const struct pci_device *d)
893{
894    int i;
895
896    for (i = 0; i < xf86NumEntities; i++) {
897	const EntityPtr p = xf86Entities[i];
898
899	if ((p->bus.type == BUS_PCI) && (p->bus.id.pci == d)) {
900	    return FALSE;
901	}
902    }
903    return TRUE;
904}
905
906
907void
908pciConvertRange2Host(int entityIndex, resRange *pRange)
909{
910    struct pci_device *const pvp = xf86GetPciInfoForEntity(entityIndex);
911    const PCITAG tag = PCI_MAKE_TAG(PCI_MAKE_BUS(pvp->domain, pvp->bus),
912				    pvp->dev, pvp->func);
913
914    if (pvp == NULL) {
915	return;
916    }
917
918    if (!(pRange->type & ResBus))
919	return;
920
921    switch(pRange->type & ResPhysMask) {
922    case ResMem:
923	switch(pRange->type & ResExtMask) {
924	case ResBlock:
925	    pRange->rBegin = pciBusAddrToHostAddr(tag,PCI_MEM, pRange->rBegin);
926	    pRange->rEnd = pciBusAddrToHostAddr(tag,PCI_MEM, pRange->rEnd);
927	    break;
928	case ResSparse:
929	    pRange->rBase = pciBusAddrToHostAddr(tag,PCI_MEM_SPARSE_BASE,
930						  pRange->rBegin);
931	    pRange->rMask = pciBusAddrToHostAddr(tag,PCI_MEM_SPARSE_MASK,
932						pRange->rEnd);
933	    break;
934	}
935	break;
936    case ResIo:
937	switch(pRange->type & ResExtMask) {
938	case ResBlock:
939	    pRange->rBegin = pciBusAddrToHostAddr(tag,PCI_IO, pRange->rBegin);
940	    pRange->rEnd = pciBusAddrToHostAddr(tag,PCI_IO, pRange->rEnd);
941	    break;
942	case ResSparse:
943	    pRange->rBase = pciBusAddrToHostAddr(tag,PCI_IO_SPARSE_BASE
944						  , pRange->rBegin);
945	    pRange->rMask = pciBusAddrToHostAddr(tag,PCI_IO_SPARSE_MASK
946						, pRange->rEnd);
947	    break;
948	}
949	break;
950    }
951
952    /* Set domain number */
953    pRange->type &= ~(ResDomain | ResBus);
954    pRange->type |= pvp->domain << 24;
955}
956