Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pci_bus.cpp « hardware « src - github.com/dosbox-staging/dosbox-staging.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 62daa206b8a1f689986648579c2f107007ef9dbc (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
/*
 *  Copyright (C) 2002-2021  The DOSBox Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */


#include "dosbox.h"
#include "inout.h"
#include "mem.h"
#include "pci_bus.h"
#include "support.h"
#include "setup.h"
#include "debug.h"
#include "callback.h"
#include "regs.h"

#if defined(PCI_FUNCTIONALITY_ENABLED)

static uint32_t pci_caddress=0;			// current PCI addressing
static Bitu pci_devices_installed=0;	// number of registered PCI devices

static uint8_t pci_cfg_data[PCI_MAX_PCIDEVICES][PCI_MAX_PCIFUNCTIONS][256];		// PCI configuration data
static PCI_Device* pci_devices[PCI_MAX_PCIDEVICES];		// registered PCI devices


// PCI address
// 31    - set for a PCI access
// 30-24 - 0
// 23-16 - bus number			(0x00ff0000)
// 15-11 - device number (slot)	(0x0000f800)
// 10- 8 - subfunction number	(0x00000700)
//  7- 2 - config register #	(0x000000fc)

static void write_pci_addr([[maybe_unused]] io_port_t port, io_val_t val, io_width_t)
{
	LOG(LOG_PCI, LOG_NORMAL)("Write PCI address :=%x", val);
	pci_caddress = val;
}

static void write_pci_register(PCI_Device* dev,uint8_t regnum,uint8_t value) {
	// vendor/device/class IDs/header type/etc. are read-only
	if ((regnum<0x04) || ((regnum>=0x06) && (regnum<0x0c)) || (regnum==0x0e)) return;
	if (dev==NULL) return;
	switch (pci_cfg_data[dev->PCIId()][dev->PCISubfunction()][0x0e]&0x7f) {	// header-type specific handling
		case 0x00:
			if ((regnum>=0x28) && (regnum<0x30)) return;	// subsystem information is read-only
			break;
		case 0x01:
		case 0x02:
		default:
			break;
	}

	// call device routine for special actions and the
	// possibility to discard/replace the value that is to be written
	Bits parsed_register=dev->ParseWriteRegister(regnum,value);
	if (parsed_register>=0)
		pci_cfg_data[dev->PCIId()][dev->PCISubfunction()][regnum]=(uint8_t)(parsed_register&0xff);
}

static void write_pci(io_port_t port, io_val_t value, io_width_t width)
{
	// write_pci is only ever registered as an 8-bit handler, despite appearing to handle up to 32-bit
	// requests. Let's check that.
	const auto val = check_cast<uint8_t>(value);
	assert(width == io_width_t::byte);

	LOG(LOG_PCI, LOG_NORMAL)("Write PCI data :=%x (io_width=%d)", port, val, static_cast<int>(width));

	// check for enabled/bus 0
	if ((pci_caddress & 0x80ff0000) == 0x80000000) {
		uint8_t devnum = (uint8_t)((pci_caddress >> 11) & 0x1f);
		uint8_t fctnum = (uint8_t)((pci_caddress >> 8) & 0x7);
		uint8_t regnum = (uint8_t)((pci_caddress & 0xfc) + (port & 0x03));
		LOG(LOG_PCI,LOG_NORMAL)("  Write to device %x register %x (function %x) (:=%x)",devnum,regnum,fctnum,val);

		if (devnum>=pci_devices_installed) return;
		PCI_Device *selected_device = pci_devices[devnum];
		if (selected_device == nullptr)
			return;
		if (fctnum > selected_device->NumSubdevices())
			return;

		PCI_Device *dev = selected_device->GetSubdevice(fctnum);
		if (dev == nullptr)
			return;

		// write data to PCI device/configuration
		switch (width) {
		case io_width_t::byte: write_pci_register(dev, regnum + 0, (uint8_t)(val & 0xff)); break;

		// WORD and DWORD are never used
		case io_width_t::word:
			write_pci_register(dev, regnum + 0, (uint8_t)(val & 0xff));
			write_pci_register(dev, regnum + 1, (uint8_t)((val >> 7 >> 1) & 0xff));
			break;
		case io_width_t::dword:
			write_pci_register(dev, regnum + 0, (uint8_t)(val & 0xff));
			write_pci_register(dev, regnum + 1, (uint8_t)((val >> 7 >> 1) & 0xff));
			write_pci_register(dev, regnum + 2, (uint8_t)((val >> 7 >> 7 >> 2) & 0xff));
			write_pci_register(dev, regnum + 3, (uint8_t)((val >> 7 >> 7 >> 7 >> 3) & 0xff));
			break;
		}
	}
}

static uint32_t read_pci_addr([[maybe_unused]] io_port_t port, io_width_t)
{
	LOG(LOG_PCI, LOG_NORMAL)("Read PCI address -> %x", pci_caddress);
	return pci_caddress;
}

// read single 8bit value from register file (special register treatment included)
static uint8_t read_pci_register(PCI_Device* dev,uint8_t regnum) {
	switch (regnum) {
		case 0x00:
			return (uint8_t)(dev->VendorID()&0xff);
		case 0x01:
			return (uint8_t)((dev->VendorID()>>8)&0xff);
		case 0x02:
			return (uint8_t)(dev->DeviceID()&0xff);
		case 0x03:
			return (uint8_t)((dev->DeviceID()>>8)&0xff);

		case 0x0e:
			return (pci_cfg_data[dev->PCIId()][dev->PCISubfunction()][regnum]&0x7f) |
						((dev->NumSubdevices()>0)?0x80:0x00);
		default:
			break;
	}

	// call device routine for special actions and possibility to discard/remap register
	Bits parsed_regnum=dev->ParseReadRegister(regnum);
	if ((parsed_regnum>=0) && (parsed_regnum<256))
		return pci_cfg_data[dev->PCIId()][dev->PCISubfunction()][parsed_regnum];

	uint8_t newval, mask;
	if (dev->OverrideReadRegister(regnum, &newval, &mask)) {
		uint8_t oldval=pci_cfg_data[dev->PCIId()][dev->PCISubfunction()][regnum] & (~mask);
		return oldval | (newval & mask);
	}

	return 0xff;
}

static uint8_t read_pci(io_port_t port, io_width_t width)
{
	// read_pci is only ever registered as an 8-bit handler, despite appearing to handle up to 32-bit
	// requests. Let's check that.
	assert(width == io_width_t::byte);

	LOG(LOG_PCI, LOG_NORMAL)("Read PCI data -> %x", pci_caddress);

	if ((pci_caddress & 0x80ff0000) == 0x80000000) {
		uint8_t devnum = (uint8_t)((pci_caddress >> 11) & 0x1f);
		uint8_t fctnum = (uint8_t)((pci_caddress >> 8) & 0x7);
		uint8_t regnum = (uint8_t)((pci_caddress & 0xfc) + (port & 0x03));
		if (devnum>=pci_devices_installed) return 0xff;
		LOG(LOG_PCI,LOG_NORMAL)("  Read from device %x register %x (function %x); addr %x",
			devnum,regnum,fctnum,pci_caddress);

		PCI_Device *selected_device = pci_devices[devnum];
		if (selected_device == nullptr)
			return 0xff;
		if (fctnum > selected_device->NumSubdevices())
			return 0xff;

		PCI_Device *dev = selected_device->GetSubdevice(fctnum);

		if (dev != nullptr) {
			switch (width) {
			case io_width_t::byte: {
				uint8_t val8 = read_pci_register(dev, regnum);
				return val8;
			}
				// WORD and DWORD are never used
			case io_width_t::word: {
				uint16_t val16 = read_pci_register(dev, regnum);
				val16 |= (read_pci_register(dev, regnum + 1) << 8);
				return (uint8_t)val16;
			}
			case io_width_t::dword: {
				uint32_t val32 = read_pci_register(dev, regnum);
				val32 |= (read_pci_register(dev, regnum + 1) << 8);
				val32 |= (read_pci_register(dev, regnum + 2) << 16);
				val32 |= (read_pci_register(dev, regnum + 3) << 24);
				return val32;
			}
			default: break;
			}
		}
	}
	return 0xff;
}

static Bitu PCI_PM_Handler() {
	LOG_MSG("PCI PMode handler, function %x",reg_ax);
	return CBRET_NONE;
}


PCI_Device::PCI_Device(uint16_t vendor, uint16_t device) {
	pci_id=-1;
	pci_subfunction=-1;
	vendor_id=vendor;
	device_id=device;
	num_subdevices=0;
	for (Bitu dct=0;dct<PCI_MAX_PCIFUNCTIONS-1;dct++) subdevices[dct]=0;
}

void PCI_Device::SetPCIId(Bitu number, Bits subfct) {
	if ((number>=0) && (number<PCI_MAX_PCIDEVICES)) {
		pci_id=number;
		if ((subfct>=0) && (subfct<PCI_MAX_PCIFUNCTIONS-1))
			pci_subfunction=subfct;
		else
			pci_subfunction=-1;
	}
}

bool PCI_Device::AddSubdevice(PCI_Device* dev) {
	if (num_subdevices<PCI_MAX_PCIFUNCTIONS-1) {
		if (subdevices[num_subdevices]!=NULL) E_Exit("PCI subdevice slot already in use!");
		subdevices[num_subdevices]=dev;
		num_subdevices++;
		return true;
	}
	return false;
}

void PCI_Device::RemoveSubdevice(Bits subfct) {
	if ((subfct>0) && (subfct<PCI_MAX_PCIFUNCTIONS)) {
		if (subfct<=this->NumSubdevices()) {
			delete subdevices[subfct-1];
			subdevices[subfct-1]=NULL;
			// should adjust things like num_subdevices as well...
		}
	}
}

PCI_Device* PCI_Device::GetSubdevice(Bits subfct) {
	if (subfct>=PCI_MAX_PCIFUNCTIONS) return NULL;
	if (subfct>0) {
		if (subfct<=this->NumSubdevices()) return subdevices[subfct-1];
	} else if (subfct==0) {
		return this;
	}
	return NULL;
}

PCI_Device::~PCI_Device() {}

// queued devices (PCI device registering requested before the PCI framework was initialized)
static const Bitu max_rqueued_devices=16;
static Bitu num_rqueued_devices=0;
static PCI_Device* rqueued_devices[max_rqueued_devices];


#include "pci_devices.h"

class PCI final : public Module_base{
private:
	bool initialized;

protected:
	IO_WriteHandleObject PCI_WriteHandler[5];
	IO_ReadHandleObject PCI_ReadHandler[5];

	CALLBACK_HandlerObject callback_pci;

public:

	PhysPt GetPModeCallbackPointer(void) {
		return Real2Phys(callback_pci.Get_RealPointer());
	}

	bool IsInitialized(void) {
		return initialized;
	}

	// set up port handlers and configuration data
	void InitializePCI(void) {
		// install PCI-addressing ports
		PCI_WriteHandler[0].Install(0xcf8, write_pci_addr, io_width_t::dword);
		PCI_ReadHandler[0].Install(0xcf8, read_pci_addr, io_width_t::dword);
		// install PCI-register read/write handlers
		for (uint8_t ct = 0; ct < 4; ++ct) {
			PCI_WriteHandler[1 + ct].Install(0xcfc + ct, write_pci, io_width_t::byte);
			PCI_ReadHandler[1 + ct].Install(0xcfc + ct, read_pci, io_width_t::byte);
		}

		for (Bitu dev=0; dev<PCI_MAX_PCIDEVICES; dev++)
			for (Bitu fct=0; fct<PCI_MAX_PCIFUNCTIONS-1; fct++)
				for (Bitu reg=0; reg<256; reg++)
					pci_cfg_data[dev][fct][reg] = 0;

		callback_pci.Install(&PCI_PM_Handler,CB_IRETD,"PCI PM");

		initialized=true;
	}

	// register PCI device to bus and setup data
	Bits RegisterPCIDevice(PCI_Device* device, Bits slot=-1) {
		if (device==NULL) return -1;

		if (slot>=0) {
			// specific slot specified, basic check for validity
			if (slot>=PCI_MAX_PCIDEVICES) return -1;
		} else {
			// auto-add to new slot, check if one is still free
			if (pci_devices_installed>=PCI_MAX_PCIDEVICES) return -1;
		}

		if (!initialized) InitializePCI();

		if (slot<0) slot=pci_devices_installed;	// use next slot
		Bits subfunction=0;	// main device unless specific already-occupied slot is requested
		if (pci_devices[slot]!=NULL) {
			subfunction=pci_devices[slot]->GetNextSubdeviceNumber();
			if (subfunction<0) E_Exit("Too many PCI subdevices!");
		}

		if (device->InitializeRegisters(pci_cfg_data[slot][subfunction])) {
			device->SetPCIId(slot, subfunction);
			if (pci_devices[slot]==NULL) {
				pci_devices[slot]=device;
				pci_devices_installed++;
			} else {
				pci_devices[slot]->AddSubdevice(device);
			}

			return slot;
		}

		return -1;
	}

	void Deinitialize(void) {
		initialized=false;
		pci_devices_installed=0;
		num_rqueued_devices=0;
		pci_caddress=0;

		for (Bitu dev=0; dev<PCI_MAX_PCIDEVICES; dev++)
			for (Bitu fct=0; fct<PCI_MAX_PCIFUNCTIONS-1; fct++)
				for (Bitu reg=0; reg<256; reg++)
					pci_cfg_data[dev][fct][reg] = 0;

		// install PCI-addressing ports
		PCI_WriteHandler[0].Uninstall();
		PCI_ReadHandler[0].Uninstall();
		// install PCI-register read/write handlers
		for (Bitu ct=0;ct<4;ct++) {
			PCI_WriteHandler[1+ct].Uninstall();
			PCI_ReadHandler[1+ct].Uninstall();
		}

		callback_pci.Uninstall();
	}

	void RemoveDevice(uint16_t vendor_id, uint16_t device_id) {
		for (Bitu dct=0;dct<pci_devices_installed;dct++) {
			if (pci_devices[dct]!=NULL) {
				if (pci_devices[dct]->NumSubdevices()>0) {
					for (Bitu sct=1;sct<PCI_MAX_PCIFUNCTIONS;sct++) {
						PCI_Device* sdev=pci_devices[dct]->GetSubdevice(sct);
						if (sdev!=NULL) {
							if ((sdev->VendorID()==vendor_id) && (sdev->DeviceID()==device_id)) {
								pci_devices[dct]->RemoveSubdevice(sct);
							}
						}
					}
				}

				if ((pci_devices[dct]->VendorID()==vendor_id) && (pci_devices[dct]->DeviceID()==device_id)) {
					delete pci_devices[dct];
					pci_devices[dct]=NULL;
				}
			}
		}

		// check if all devices have been removed
		bool any_device_left=false;
		for (Bitu dct=0;dct<PCI_MAX_PCIDEVICES;dct++) {
			if (dct>=pci_devices_installed) break;
			if (pci_devices[dct]!=NULL) {
				any_device_left=true;
				break;
			}
		}
		if (!any_device_left) Deinitialize();

		Bitu last_active_device=PCI_MAX_PCIDEVICES;
		for (Bitu dct=0;dct<PCI_MAX_PCIDEVICES;dct++) {
			if (pci_devices[dct]!=NULL) last_active_device=dct;
		}
		if (last_active_device<pci_devices_installed)
			pci_devices_installed=last_active_device+1;
	}

	PCI(Section* configuration):Module_base(configuration) {
		initialized=false;
		pci_devices_installed=0;

		for (Bitu devct=0;devct<PCI_MAX_PCIDEVICES;devct++)
			pci_devices[devct]=NULL;

		if (num_rqueued_devices>0) {
			// register all devices that have been added before the PCI bus was instantiated
			for (Bitu dct=0;dct<num_rqueued_devices;dct++) {
				this->RegisterPCIDevice(rqueued_devices[dct]);
			}
			num_rqueued_devices=0;
		}
	}

	~PCI(){
		initialized=false;
		pci_devices_installed=0;
		num_rqueued_devices=0;
	}

};

static PCI* pci_interface=NULL;

void PCI_AddSST_Device(Bitu type) {
	Bitu ctype = 1;
	switch (type) {
		case 1:
		case 2:
			ctype = type;
			break;
		default:
			LOG_MSG("PCI:SST: Invalid board type %x specified", (unsigned int)type);
			break;
	}
	PCI_Device* voodoo_dev=new PCI_SSTDevice(ctype);
	if (pci_interface!=NULL) {
		pci_interface->RegisterPCIDevice(voodoo_dev);
	} else {
		if (num_rqueued_devices<max_rqueued_devices)
			rqueued_devices[num_rqueued_devices++]=voodoo_dev;
	}
}

void PCI_RemoveSST_Device(void) {
	if (pci_interface!=NULL) {
		uint16_t vendor=PCI_SSTDevice::VendorID();
		pci_interface->RemoveDevice(vendor,1);
		pci_interface->RemoveDevice(vendor,2);
	}
}

PhysPt PCI_GetPModeInterface(void) {
	if (pci_interface) {
		return pci_interface->GetPModeCallbackPointer();
	}
	return 0;
}

bool PCI_IsInitialized() {
	if (pci_interface) return pci_interface->IsInitialized();
	return false;
}

void PCI_ShutDown(Section *){
	delete pci_interface;
	pci_interface=NULL;
}

void PCI_Init(Section* sec) {
	pci_interface = new PCI(sec);
	sec->AddDestroyFunction(&PCI_ShutDown,false);
}

#else

void PCI_AddSST_Device(Bitu type) {
}

void PCI_RemoveSST_Device(void) {
}

#endif