Copyright 1999, Be Incorporated. All Rights Reserved.
This file may be used under the terms of the Be Sample Code License.
Other authors:
Mark Watson;
Rudolf Cornelissen 3/2002-4/2006.
*/
#include <KernelExport.h>
#include <ISA.h>
#include <PCI.h>
#include <OS.h>
#include <directories.h>
#include <driver_settings.h>
#include <malloc.h>
#include <stdlib.h>
#include "AGP.h"
#include <graphic_driver.h>
#include <stdio.h>
#include <string.h>
#include "DriverInterface.h"
#include "macros.h"
#define get_pci(o, s) (*pci_bus->read_pci_config)(pcii->bus, pcii->device, pcii->function, (o), (s))
#define set_pci(o, s, v) (*pci_bus->write_pci_config)(pcii->bus, pcii->device, pcii->function, (o), (s), (v))
#define MAX_DEVICES 8
#define DEVICE_FORMAT "%04x_%04x_%02x%02x%02x" // apsed
int32 api_version = B_CUR_DRIVER_API_VERSION;
typedef struct device_info device_info;
typedef struct {
timer te;
device_info *di;
bigtime_t when_target;
} timer_info;
struct device_info {
uint32 is_open;
area_id shared_area;
shared_info *si;
vuint32 *regs;
pci_info pcii;
char name[B_OS_NAME_LENGTH];
};
typedef struct {
uint32 count;
benaphore kernel;
char *device_names[MAX_DEVICES+1];
device_info di[MAX_DEVICES];
} DeviceData;
static status_t open_hook (const char* name, uint32 flags, void** cookie);
static status_t close_hook (void* dev);
static status_t free_hook (void* dev);
static status_t read_hook (void* dev, off_t pos, void* buf, size_t* len);
static status_t write_hook (void* dev, off_t pos, const void* buf, size_t* len);
static status_t control_hook (void* dev, uint32 msg, void *buf, size_t len);
static status_t map_device(device_info *di);
static void unmap_device(device_info *di);
static void probe_devices(void);
static int32 eng_interrupt(void *data);
static DeviceData *pd;
static isa_module_info *isa_bus = NULL;
static pci_module_info *pci_bus = NULL;
static agp_module_info *agp_bus = NULL;
static device_hooks graphics_device_hooks = {
open_hook,
close_hook,
free_hook,
control_hook,
read_hook,
write_hook,
NULL,
NULL,
NULL,
NULL
};
#define VENDOR_ID_NVIDIA 0x1106 /* Via */
static uint16 nvidia_device_list[] = {
0x3122,
0
};
static struct {
uint16 vendor;
uint16 *devices;
} SupportedDevices[] = {
{0x0000, NULL}
};
static settings current_settings = {
DRIVER_PREFIX ".accelerant",
false,
0x00000000,
0,
true,
true,
false,
false,
false,
true,
};
static void dumprom (void *rom, uint32 size)
{
int fd;
uint32 cnt;
fd = open (kUserDirectory "/" DRIVER_PREFIX ".rom",
O_WRONLY | O_CREAT, 0666);
if (fd < 0) return;
* the ROM size is a multiple of that anyway. */
for (cnt = 0; (cnt < size); cnt += 32768)
write (fd, ((void *)(((uint8 *)rom) + cnt)), 32768);
close (fd);
}
static int caused_vbi(vuint32 * regs)
{
return 0;
}
static void clear_vbi(vuint32 * regs)
{
}
static void enable_vbi(vuint32 * regs)
{
}
static void disable_vbi(vuint32 * regs)
{
}
init_hardware() - Returns B_OK if one is
found, otherwise returns B_ERROR so the driver will be unloaded.
*/
status_t
init_hardware(void) {
long pci_index = 0;
pci_info pcii;
bool found_one = false;
if (get_module(B_PCI_MODULE_NAME, (module_info **)&pci_bus) != B_OK)
return B_ERROR;
if (get_module(B_ISA_MODULE_NAME, (module_info **)&isa_bus) != B_OK)
{
put_module(B_PCI_MODULE_NAME);
return B_ERROR;
}
while ((*pci_bus->get_nth_pci_info)(pci_index, &pcii) == B_NO_ERROR) {
int vendor = 0;
while (SupportedDevices[vendor].vendor) {
if (SupportedDevices[vendor].vendor == pcii.vendor_id) {
uint16 *devices = SupportedDevices[vendor].devices;
while (*devices) {
if (*devices == pcii.device_id ) {
found_one = true;
goto done;
}
devices++;
}
}
vendor++;
}
pci_index++;
}
done:
put_module(B_PCI_MODULE_NAME);
return (found_one ? B_OK : B_ERROR);
}
status_t
init_driver(void) {
void *settings_handle;
settings_handle = load_driver_settings (DRIVER_PREFIX ".settings");
if (settings_handle != NULL) {
const char *item;
char *end;
uint32 value;
item = get_driver_parameter (settings_handle, "accelerant", "", "");
if ((strlen (item) > 0) && (strlen (item) < sizeof (current_settings.accelerant) - 1)) {
strcpy (current_settings.accelerant, item);
}
current_settings.dumprom = get_driver_boolean_parameter (settings_handle, "dumprom", false, false);
item = get_driver_parameter (settings_handle, "logmask", "0x00000000", "0x00000000");
value = strtoul (item, &end, 0);
if (*end == '\0') current_settings.logmask = value;
item = get_driver_parameter (settings_handle, "memory", "0", "0");
value = strtoul (item, &end, 0);
if (*end == '\0') current_settings.memory = value;
current_settings.hardcursor = get_driver_boolean_parameter (settings_handle, "hardcursor", false, false);
current_settings.usebios = get_driver_boolean_parameter (settings_handle, "usebios", false, false);
current_settings.switchhead = get_driver_boolean_parameter (settings_handle, "switchhead", false, false);
current_settings.force_pci = get_driver_boolean_parameter (settings_handle, "force_pci", false, false);
current_settings.unhide_fw = get_driver_boolean_parameter (settings_handle, "unhide_fw", false, false);
current_settings.pgm_panel = get_driver_boolean_parameter (settings_handle, "pgm_panel", false, false);
unload_driver_settings (settings_handle);
}
if (get_module(B_PCI_MODULE_NAME, (module_info **)&pci_bus) != B_OK)
return B_ERROR;
if (get_module(B_ISA_MODULE_NAME, (module_info **)&isa_bus) != B_OK)
{
put_module(B_PCI_MODULE_NAME);
return B_ERROR;
}
get_module(B_AGP_MODULE_NAME, (module_info **)&agp_bus);
pd = (DeviceData *)calloc(1, sizeof(DeviceData));
if (!pd) {
put_module(B_PCI_MODULE_NAME);
return B_ERROR;
}
INIT_BEN(pd->kernel);
probe_devices();
return B_OK;
}
const char **
publish_devices(void) {
return (const char **)pd->device_names;
}
device_hooks *
find_device(const char *name) {
int index = 0;
while (pd->device_names[index]) {
if (strcmp(name, pd->device_names[index]) == 0)
return &graphics_device_hooks;
index++;
}
return NULL;
}
void uninit_driver(void) {
DELETE_BEN(pd->kernel);
free(pd);
pd = NULL;
put_module(B_PCI_MODULE_NAME);
put_module(B_ISA_MODULE_NAME);
if (agp_bus) put_module(B_AGP_MODULE_NAME);
}
static status_t map_device(device_info *di)
{
char buffer[B_OS_NAME_LENGTH];
shared_info *si = di->si;
uint32 tmpUlong;
pci_info *pcii = &(di->pcii);
system_info sysinfo;
uint8* rom_temp;
area_id rom_area;
int registers = 1;
int frame_buffer = 0;
tmpUlong = get_pci(PCI_command, 2);
tmpUlong |= PCI_command_memory;
tmpUlong |= PCI_command_master;
tmpUlong &= ~PCI_command_io;
set_pci(PCI_command, 2, tmpUlong);
get_system_info(&sysinfo);
if (0)
{
si->use_clone_bugfix = 1;
}
else
{
si->use_clone_bugfix = 0;
}
sprintf(buffer, DEVICE_FORMAT " regs",
di->pcii.vendor_id, di->pcii.device_id,
di->pcii.bus, di->pcii.device, di->pcii.function);
si->regs_area = map_physical_memory(
buffer,
di->pcii.u.h0.base_registers_pci[registers],
di->pcii.u.h0.base_register_sizes[registers],
B_ANY_KERNEL_ADDRESS,
B_CLONEABLE_AREA | (si->use_clone_bugfix ? B_READ_AREA|B_WRITE_AREA : 0),
(void **)&(di->regs));
si->clone_bugfix_regs = (uint32 *) di->regs;
if (si->regs_area < 0) return si->regs_area;
sprintf(buffer, DEVICE_FORMAT " rom",
di->pcii.vendor_id, di->pcii.device_id,
di->pcii.bus, di->pcii.device, di->pcii.function);
* don't touch: (confirmed) NV04, NV05, NV05-M64, NV11 all shutoff otherwise.
* NV18, NV28 and NV34 keep working.
* confirmed NV28 and NV34 to use upper part of shadowed ROM for scratch purposes,
* however the actual ROM content (so the used part) is intact (confirmed). */
tmpUlong = get_pci(PCI_rom_base, 4);
if (tmpUlong)
{
tmpUlong |= 0x00000001;
set_pci(PCI_rom_base, 4, tmpUlong);
rom_area = map_physical_memory(
buffer,
di->pcii.u.h0.rom_base_pci,
di->pcii.u.h0.rom_size,
B_ANY_KERNEL_ADDRESS,
B_READ_AREA,
(void **)&(rom_temp)
);
if (rom_temp[0]!=0x55 || rom_temp[1]!=0xaa)
{
delete_area(rom_area);
rom_area = -1;
tmpUlong = 0x00000000;
}
}
if (!tmpUlong)
{
rom_area = map_physical_memory(
buffer,
0x000c0000,
65536,
B_ANY_KERNEL_ADDRESS,
B_READ_AREA,
(void **)&(rom_temp)
);
}
if (rom_area < 0) {
delete_area(si->regs_area);
si->regs_area = -1;
return rom_area;
}
* (ROM always fits in 64Kb: checked TNT1 - FX5950) */
if (current_settings.dumprom) dumprom (rom_temp, 65536);
memcpy (si->rom_mirror, rom_temp, 65536);
tmpUlong = get_pci(PCI_rom_base, 4);
tmpUlong &= 0xfffffffe;
set_pci(PCI_rom_base, 4, tmpUlong);
delete_area(rom_area);
sprintf(buffer, DEVICE_FORMAT " framebuffer",
di->pcii.vendor_id, di->pcii.device_id,
di->pcii.bus, di->pcii.device, di->pcii.function);
si->fb_area = map_physical_memory(
buffer,
di->pcii.u.h0.base_registers_pci[frame_buffer],
di->pcii.u.h0.base_register_sizes[frame_buffer],
B_ANY_KERNEL_BLOCK_ADDRESS | B_WRITE_COMBINING_MEMORY,
B_READ_AREA | B_WRITE_AREA | B_CLONEABLE_AREA,
&(si->framebuffer));
if (si->fb_area < 0) {
si->fb_area = map_physical_memory(
buffer,
di->pcii.u.h0.base_registers_pci[frame_buffer],
di->pcii.u.h0.base_register_sizes[frame_buffer],
B_ANY_KERNEL_BLOCK_ADDRESS,
B_READ_AREA | B_WRITE_AREA | B_CLONEABLE_AREA,
&(si->framebuffer));
}
if (si->fb_area < 0)
{
delete_area(si->regs_area);
si->regs_area = -1;
return si->fb_area;
}
si->framebuffer_pci = (void *) di->pcii.u.h0.base_registers_pci[frame_buffer];
si->settings = current_settings;
return si->fb_area;
}
static void unmap_device(device_info *di) {
shared_info *si = di->si;
uint32 tmpUlong;
pci_info *pcii = &(di->pcii);
tmpUlong = get_pci(PCI_command, 4);
tmpUlong &= 0xfffffffc;
set_pci(PCI_command, 4, tmpUlong);
if (si->regs_area >= 0) delete_area(si->regs_area);
if (si->fb_area >= 0) delete_area(si->fb_area);
si->regs_area = si->fb_area = -1;
si->framebuffer = NULL;
di->regs = NULL;
}
static void probe_devices(void) {
uint32 pci_index = 0;
uint32 count = 0;
device_info *di = pd->di;
while ((count < MAX_DEVICES) && ((*pci_bus->get_nth_pci_info)(pci_index, &(di->pcii)) == B_NO_ERROR)) {
int vendor = 0;
while (SupportedDevices[vendor].vendor) {
if (SupportedDevices[vendor].vendor == di->pcii.vendor_id) {
uint16 *devices = SupportedDevices[vendor].devices;
while (*devices) {
if (*devices == di->pcii.device_id ) {
sprintf(di->name, "graphics/" DEVICE_FORMAT,
di->pcii.vendor_id, di->pcii.device_id,
di->pcii.bus, di->pcii.device, di->pcii.function);
pd->device_names[count] = di->name;
di->is_open = 0;
di->shared_area = -1;
di->si = NULL;
di++;
count++;
goto next_device;
}
devices++;
}
}
vendor++;
}
next_device:
pci_index++;
}
pd->count = count;
pd->device_names[pd->count] = NULL;
}
static uint32 thread_interrupt_work(int32 *flags, vuint32 *regs, shared_info *si) {
uint32 handled = B_HANDLED_INTERRUPT;
if (si->vblank >= 0) {
int32 blocked;
if ((get_sem_count(si->vblank, &blocked) == B_OK) && (blocked < 0)) {
release_sem_etc(si->vblank, -blocked, B_DO_NOT_RESCHEDULE);
handled = B_INVOKE_SCHEDULER;
}
}
return handled;
}
static int32
eng_interrupt(void *data)
{
int32 handled = B_UNHANDLED_INTERRUPT;
device_info *di = (device_info *)data;
shared_info *si = di->si;
int32 *flags = &(si->flags);
vuint32 *regs;
if (atomic_or(flags, SKD_HANDLER_INSTALLED) & SKD_HANDLER_INSTALLED) {
goto exit0;
}
regs = di->regs;
if (caused_vbi(regs)) {
clear_vbi(regs);
handled = thread_interrupt_work(flags, regs, si);
}
atomic_and(flags, ~SKD_HANDLER_INSTALLED);
exit0:
return handled;
}
static status_t open_hook (const char* name, uint32 flags, void** cookie) {
int32 index = 0;
device_info *di;
shared_info *si;
thread_id thid;
thread_info thinfo;
status_t result = B_OK;
vuint32 *regs;
char shared_name[B_OS_NAME_LENGTH];
while (pd->device_names[index] && (strcmp(name, pd->device_names[index]) != 0)) index++;
di = &(pd->di[index]);
AQUIRE_BEN(pd->kernel);
if (di->is_open) {
goto mark_as_open;
}
sprintf(shared_name, DEVICE_FORMAT " shared",
di->pcii.vendor_id, di->pcii.device_id,
di->pcii.bus, di->pcii.device, di->pcii.function);
di->shared_area = create_area(shared_name, (void **)&(di->si), B_ANY_KERNEL_ADDRESS, ((sizeof(shared_info) + (B_PAGE_SIZE - 1)) & ~(B_PAGE_SIZE - 1)), B_FULL_LOCK,
B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA | B_CLONEABLE_AREA);
if (di->shared_area < 0) {
result = di->shared_area;
goto done;
}
si = di->si;
si->vendor_id = di->pcii.vendor_id;
si->device_id = di->pcii.device_id;
si->revision = di->pcii.revision;
si->bus = di->pcii.bus;
si->device = di->pcii.device;
si->function = di->pcii.function;
* unified memory architecture (UMA) */
switch ((((uint32)(si->device_id)) << 16) | si->vendor_id)
{
case 0x01a010de:
si->ps.memory_size = 1024 * 1024 *
(((((*pci_bus->read_pci_config)(0, 0, 1, 0x7c, 4)) & 0x000007c0) >> 6) + 1);
si->ps.memory_size -= (64 * 1024);
break;
case 0x01f010de:
si->ps.memory_size = 1024 * 1024 *
(((((*pci_bus->read_pci_config)(0, 0, 1, 0x84, 4)) & 0x000007f0) >> 4) + 1);
si->ps.memory_size -= (64 * 1024);
break;
default:
* accelerant. */
break;
}
result = map_device(di);
if (result < 0) goto free_shared;
result = B_OK;
si->vblank = create_sem(0, di->name);
if (si->vblank < 0) {
result = si->vblank;
goto unmap;
}
thid = find_thread(NULL);
get_thread_info(thid, &thinfo);
set_sem_owner(si->vblank, thinfo.team);
regs = di->regs;
disable_vbi(regs);
if ((di->pcii.u.h0.interrupt_pin == 0x00) ||
(di->pcii.u.h0.interrupt_line == 0xff) ||
(di->pcii.u.h0.interrupt_line <= 0x02))
{
result = B_ERROR;
goto delete_the_sem;
}
else
{
result = install_io_interrupt_handler(di->pcii.u.h0.interrupt_line, eng_interrupt, (void *)di, 0);
if (result != B_OK) goto delete_the_sem;
}
mark_as_open:
di->is_open++;
*cookie = di;
goto done;
delete_the_sem:
delete_sem(si->vblank);
unmap:
unmap_device(di);
free_shared:
delete_area(di->shared_area);
di->shared_area = -1;
di->si = NULL;
done:
RELEASE_BEN(pd->kernel);
return result;
}
read_hook - does nothing, gracefully
----- */
static status_t
read_hook (void* dev, off_t pos, void* buf, size_t* len)
{
*len = 0;
return B_NOT_ALLOWED;
}
write_hook - does nothing, gracefully
----- */
static status_t
write_hook (void* dev, off_t pos, const void* buf, size_t* len)
{
*len = 0;
return B_NOT_ALLOWED;
}
close_hook - does nothing, gracefully
----- */
static status_t
close_hook (void* dev)
{
return B_NO_ERROR;
}
free_hook - close down the device
----------- */
static status_t
free_hook (void* dev) {
device_info *di = (device_info *)dev;
shared_info *si = di->si;
vuint32 *regs = di->regs;
AQUIRE_BEN(pd->kernel);
if (di->is_open > 1)
goto unlock_and_exit;
disable_vbi(regs);
remove_io_interrupt_handler(di->pcii.u.h0.interrupt_line, eng_interrupt, di);
delete_sem(si->vblank);
si->vblank = -1;
unmap_device(di);
delete_area(di->shared_area);
di->shared_area = -1;
di->si = NULL;
unlock_and_exit:
di->is_open--;
RELEASE_BEN(pd->kernel);
return B_OK;
}
control_hook - where the real work is done
----------- */
static status_t
control_hook (void* dev, uint32 msg, void *buf, size_t len) {
device_info *di = (device_info *)dev;
status_t result = B_DEV_INVALID_IOCTL;
uint32 tmpUlong;
switch (msg) {
case B_GET_ACCELERANT_SIGNATURE: {
char *sig = (char *)buf;
strcpy(sig, current_settings.accelerant);
result = B_OK;
} break;
case ENG_GET_PRIVATE_DATA: {
eng_get_private_data *gpd = (eng_get_private_data *)buf;
if (gpd->magic == SKEL_PRIVATE_DATA_MAGIC) {
gpd->shared_info_area = di->shared_area;
result = B_OK;
}
} break;
case ENG_GET_PCI: {
eng_get_set_pci *gsp = (eng_get_set_pci *)buf;
if (gsp->magic == SKEL_PRIVATE_DATA_MAGIC) {
pci_info *pcii = &(di->pcii);
gsp->value = get_pci(gsp->offset, gsp->size);
result = B_OK;
}
} break;
case ENG_SET_PCI: {
eng_get_set_pci *gsp = (eng_get_set_pci *)buf;
if (gsp->magic == SKEL_PRIVATE_DATA_MAGIC) {
pci_info *pcii = &(di->pcii);
set_pci(gsp->offset, gsp->size, gsp->value);
result = B_OK;
}
} break;
case ENG_DEVICE_NAME: {
eng_device_name *dn = (eng_device_name *)buf;
if (dn->magic == SKEL_PRIVATE_DATA_MAGIC) {
strcpy(dn->name, di->name);
result = B_OK;
}
} break;
case ENG_RUN_INTERRUPTS: {
eng_set_bool_state *ri = (eng_set_bool_state *)buf;
if (ri->magic == SKEL_PRIVATE_DATA_MAGIC) {
vuint32 *regs = di->regs;
if (ri->do_it) {
enable_vbi(regs);
} else {
disable_vbi(regs);
}
result = B_OK;
}
} break;
case ENG_GET_NTH_AGP_INFO: {
eng_nth_agp_info *nai = (eng_nth_agp_info *)buf;
if (nai->magic == SKEL_PRIVATE_DATA_MAGIC) {
nai->exist = false;
nai->agp_bus = false;
if (agp_bus) {
nai->agp_bus = true;
if ((*agp_bus->get_nth_agp_info)(nai->index, &(nai->agpi)) == B_NO_ERROR) {
nai->exist = true;
}
}
result = B_OK;
}
} break;
case ENG_ENABLE_AGP: {
eng_cmd_agp *nca = (eng_cmd_agp *)buf;
if (nca->magic == SKEL_PRIVATE_DATA_MAGIC) {
if (agp_bus) {
nca->agp_bus = true;
(*agp_bus->enable_agp)(&(nca->cmd));
} else {
nca->agp_bus = false;
nca->cmd = 0;
}
result = B_OK;
}
} break;
case ENG_ISA_OUT: {
eng_in_out_isa *io_isa = (eng_in_out_isa *)buf;
if (io_isa->magic == SKEL_PRIVATE_DATA_MAGIC) {
pci_info *pcii = &(di->pcii);
* no other graphics card may have ISA I/O enabled when we enter */
AQUIRE_BEN(pd->kernel);
tmpUlong = get_pci(PCI_command, 2);
tmpUlong |= PCI_command_io;
set_pci(PCI_command, 2, tmpUlong);
if (io_isa->size == 1)
isa_bus->write_io_8(io_isa->adress, (uint8)io_isa->data);
else
isa_bus->write_io_16(io_isa->adress, io_isa->data);
result = B_OK;
tmpUlong = get_pci(PCI_command, 2);
tmpUlong &= ~PCI_command_io;
set_pci(PCI_command, 2, tmpUlong);
RELEASE_BEN(pd->kernel);
}
} break;
case ENG_ISA_IN: {
eng_in_out_isa *io_isa = (eng_in_out_isa *)buf;
if (io_isa->magic == SKEL_PRIVATE_DATA_MAGIC) {
pci_info *pcii = &(di->pcii);
* no other graphics card may have ISA I/O enabled when we enter */
AQUIRE_BEN(pd->kernel);
tmpUlong = get_pci(PCI_command, 2);
tmpUlong |= PCI_command_io;
set_pci(PCI_command, 2, tmpUlong);
if (io_isa->size == 1)
io_isa->data = isa_bus->read_io_8(io_isa->adress);
else
io_isa->data = isa_bus->read_io_16(io_isa->adress);
result = B_OK;
tmpUlong = get_pci(PCI_command, 2);
tmpUlong &= ~PCI_command_io;
set_pci(PCI_command, 2, tmpUlong);
RELEASE_BEN(pd->kernel);
}
} break;
}
return result;
}