1 /* 2 * Copyright 2006-2009, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Axel Dörfler, axeld@pinc-software.de 7 */ 8 9 10 #include "driver.h" 11 #include "device.h" 12 #include "lock.h" 13 14 #include <stdlib.h> 15 #include <stdio.h> 16 #include <string.h> 17 18 #include <AGP.h> 19 #include <KernelExport.h> 20 #include <OS.h> 21 #include <PCI.h> 22 #include <SupportDefs.h> 23 24 25 #define TRACE_DRIVER 26 #ifdef TRACE_DRIVER 27 # define TRACE(x...) dprintf("intel_extreme: " x) 28 #else 29 # define TRACE(x) ; 30 #endif 31 32 #define ERROR(x...) dprintf("intel_extreme: " x) 33 #define CALLED(x...) TRACE("CALLED %s\n", __PRETTY_FUNCTION__) 34 35 36 #define MAX_CARDS 4 37 38 39 // list of supported devices 40 const struct supported_device { 41 uint32 device_id; 42 int32 type; 43 const char* name; 44 } kSupportedDevices[] = { 45 {0x3577, INTEL_GROUP_83x, "i830GM"}, 46 {0x2562, INTEL_GROUP_83x, "i845G"}, 47 48 {0x2572, INTEL_GROUP_85x, "i865G"}, 49 {0x3582, INTEL_GROUP_85x, "i855G"}, 50 {0x358e, INTEL_GROUP_85x, "i855G"}, 51 52 {0x2582, INTEL_MODEL_915, "i915G"}, 53 {0x258a, INTEL_MODEL_915, "i915"}, 54 {0x2592, INTEL_MODEL_915M, "i915GM"}, 55 {0x2792, INTEL_MODEL_915, "i910"}, 56 {0x2772, INTEL_MODEL_945, "i945G"}, 57 {0x27a2, INTEL_MODEL_945M, "i945GM"}, 58 {0x27ae, INTEL_MODEL_945M, "i945GME"}, 59 {0x2972, INTEL_MODEL_965, "i946G"}, 60 {0x2982, INTEL_MODEL_965, "G35"}, 61 {0x2992, INTEL_MODEL_965, "i965Q"}, 62 {0x29a2, INTEL_MODEL_965, "i965G"}, 63 {0x2a02, INTEL_MODEL_965M, "i965GM"}, 64 {0x2a12, INTEL_MODEL_965M, "i965GME"}, 65 {0x29b2, INTEL_MODEL_G33, "G33G"}, 66 {0x29c2, INTEL_MODEL_G33, "Q35G"}, 67 {0x29d2, INTEL_MODEL_G33, "Q33G"}, 68 69 {0x2a42, INTEL_MODEL_GM45, "GM45"}, 70 {0x2e02, INTEL_MODEL_G45, "IGD"}, 71 {0x2e12, INTEL_MODEL_G45, "Q45"}, 72 {0x2e22, INTEL_MODEL_G45, "G45"}, 73 {0x2e32, INTEL_MODEL_G45, "G41"}, 74 {0x2e42, INTEL_MODEL_G45, "B43"}, 75 {0x2e92, INTEL_MODEL_G45, "B43"}, 76 77 {0xa001, INTEL_MODEL_PINE, "Atom D4xx"}, 78 {0xa002, INTEL_MODEL_PINE, "Atom D5xx"}, 79 {0xa011, INTEL_MODEL_PINEM, "Atom N4xx"}, 80 {0xa012, INTEL_MODEL_PINEM, "Atom N5xx"}, 81 82 {0x0042, INTEL_MODEL_ILKG, "IronLake Desktop"}, 83 {0x0046, INTEL_MODEL_ILKGM, "IronLake Mobile"}, 84 {0x0046, INTEL_MODEL_ILKGM, "IronLake Mobile"}, 85 {0x0046, INTEL_MODEL_ILKGM, "IronLake Mobile"}, 86 87 {0x0102, INTEL_MODEL_SNBG, "SandyBridge Desktop GT1"}, 88 {0x0112, INTEL_MODEL_SNBG, "SandyBridge Desktop GT2"}, 89 {0x0122, INTEL_MODEL_SNBG, "SandyBridge Desktop GT2+"}, 90 {0x0106, INTEL_MODEL_SNBGM, "SandyBridge Mobile GT1"}, 91 {0x0116, INTEL_MODEL_SNBGM, "SandyBridge Mobile GT2"}, 92 {0x0126, INTEL_MODEL_SNBGM, "SandyBridge Mobile GT2+"}, 93 {0x010a, INTEL_MODEL_SNBGS, "SandyBridge Server"}, 94 95 {0x0152, INTEL_MODEL_IVBG, "IvyBridge Desktop GT1"}, 96 {0x0162, INTEL_MODEL_IVBG, "IvyBridge Desktop GT2"}, 97 {0x0156, INTEL_MODEL_IVBGM, "IvyBridge Mobile GT1"}, 98 {0x0166, INTEL_MODEL_IVBGM, "IvyBridge Mobile GT2"}, 99 {0x015a, INTEL_MODEL_IVBGS, "IvyBridge Server GT1"}, 100 {0x016a, INTEL_MODEL_IVBGS, "IvyBridge Server GT2"}, 101 102 {0x0412, INTEL_MODEL_HAS, "Haswell Desktop"}, 103 {0x0416, INTEL_MODEL_HASM, "Haswell Mobile"}, 104 {0x0d26, INTEL_MODEL_HASM, "Haswell Mobile"}, 105 {0x0a16, INTEL_MODEL_HASM, "Haswell Mobile"}, 106 107 {0x0155, INTEL_MODEL_VLV, "ValleyView Desktop"}, 108 {0x0f30, INTEL_MODEL_VLVM, "ValleyView Mobile"}, 109 {0x0f31, INTEL_MODEL_VLVM, "ValleyView Mobile"}, 110 {0x0f32, INTEL_MODEL_VLVM, "ValleyView Mobile"}, 111 {0x0f33, INTEL_MODEL_VLVM, "ValleyView Mobile"}, 112 {0x0157, INTEL_MODEL_VLVM, "ValleyView Mobile"}, 113 114 {0x1902, INTEL_MODEL_SKY, "Skylake GT1"}, 115 {0x1906, INTEL_MODEL_SKYM, "Skylake GT1"}, 116 {0x190a, INTEL_MODEL_SKYS, "Skylake GT1"}, 117 {0x190b, INTEL_MODEL_SKY, "Skylake GT1"}, 118 {0x190e, INTEL_MODEL_SKYM, "Skylake GT1"}, 119 {0x1912, INTEL_MODEL_SKY, "Skylake GT2"}, 120 {0x1916, INTEL_MODEL_SKYM, "Skylake GT2"}, 121 {0x191a, INTEL_MODEL_SKYS, "Skylake GT2"}, 122 {0x191b, INTEL_MODEL_SKY, "Skylake GT2"}, 123 {0x191d, INTEL_MODEL_SKY, "Skylake GT2"}, 124 {0x191e, INTEL_MODEL_SKYM, "Skylake GT2"}, 125 {0x1921, INTEL_MODEL_SKYM, "Skylake GT2F"}, 126 {0x1926, INTEL_MODEL_SKYM, "Skylake GT3"}, 127 {0x192a, INTEL_MODEL_SKYS, "Skylake GT3"}, 128 {0x192b, INTEL_MODEL_SKY, "Skylake GT3"}, 129 }; 130 131 int32 api_version = B_CUR_DRIVER_API_VERSION; 132 133 char* gDeviceNames[MAX_CARDS + 1]; 134 intel_info* gDeviceInfo[MAX_CARDS]; 135 pci_module_info* gPCI; 136 pci_x86_module_info* gPCIx86Module = NULL; 137 agp_gart_module_info* gGART; 138 mutex gLock; 139 140 141 static status_t 142 get_next_intel_extreme(int32* _cookie, pci_info &info, uint32 &type) 143 { 144 int32 index = *_cookie; 145 146 // find devices 147 148 for (; gPCI->get_nth_pci_info(index, &info) == B_OK; index++) { 149 // check vendor 150 if (info.vendor_id != VENDOR_ID_INTEL 151 || info.class_base != PCI_display 152 || info.class_sub != PCI_vga) 153 continue; 154 155 // check device 156 for (uint32 i = 0; i < sizeof(kSupportedDevices) 157 / sizeof(kSupportedDevices[0]); i++) { 158 if (info.device_id == kSupportedDevices[i].device_id) { 159 type = i; 160 *_cookie = index + 1; 161 return B_OK; 162 } 163 } 164 } 165 166 return B_ENTRY_NOT_FOUND; 167 } 168 169 170 static enum pch_info 171 detect_intel_pch() 172 { 173 pci_info info; 174 175 // find devices 176 for (int32 i = 0; gPCI->get_nth_pci_info(i, &info) == B_OK; i++) { 177 // check vendor 178 if (info.vendor_id != VENDOR_ID_INTEL 179 || info.class_base != PCI_bridge 180 || info.class_sub != PCI_isa) { 181 continue; 182 } 183 184 // check device 185 unsigned short id = info.device_id & INTEL_PCH_DEVICE_ID_MASK; 186 switch(id) { 187 case INTEL_PCH_IBX_DEVICE_ID: 188 ERROR("%s: Found Ibex Peak PCH\n", __func__); 189 return INTEL_PCH_IBX; 190 case INTEL_PCH_CPT_DEVICE_ID: 191 ERROR("%s: Found CougarPoint PCH\n", __func__); 192 return INTEL_PCH_CPT; 193 case INTEL_PCH_PPT_DEVICE_ID: 194 ERROR("%s: Found PantherPoint PCH\n", __func__); 195 return INTEL_PCH_CPT; 196 case INTEL_PCH_LPT_DEVICE_ID: 197 ERROR("%s: Found LynxPoint PCH\n", __func__); 198 return INTEL_PCH_LPT; 199 case INTEL_PCH_SPT_DEVICE_ID: 200 case INTEL_PCH_SPT_LP_DEVICE_ID: 201 ERROR("%s: Found SunrisePoint PCH\n", __func__); 202 return INTEL_PCH_SPT; 203 } 204 } 205 206 ERROR("%s: No PCH detected.\n", __func__); 207 return INTEL_PCH_NONE; 208 } 209 210 211 extern "C" const char** 212 publish_devices(void) 213 { 214 CALLED(); 215 return (const char**)gDeviceNames; 216 } 217 218 219 extern "C" status_t 220 init_hardware(void) 221 { 222 CALLED(); 223 224 status_t status = get_module(B_PCI_MODULE_NAME,(module_info**)&gPCI); 225 if (status != B_OK) { 226 ERROR("pci module unavailable\n"); 227 return status; 228 } 229 230 int32 cookie = 0; 231 uint32 type; 232 pci_info info; 233 status = get_next_intel_extreme(&cookie, info, type); 234 235 put_module(B_PCI_MODULE_NAME); 236 return status; 237 } 238 239 240 extern "C" status_t 241 init_driver(void) 242 { 243 CALLED(); 244 245 status_t status = get_module(B_PCI_MODULE_NAME, (module_info**)&gPCI); 246 if (status != B_OK) { 247 ERROR("pci module unavailable\n"); 248 return status; 249 } 250 251 status = get_module(B_AGP_GART_MODULE_NAME, (module_info**)&gGART); 252 if (status != B_OK) { 253 ERROR("AGP GART module unavailable\n"); 254 put_module(B_PCI_MODULE_NAME); 255 return status; 256 } 257 258 mutex_init(&gLock, "intel extreme ksync"); 259 260 // Try to get the PCI x86 module as well so we can enable possible MSIs. 261 if (get_module(B_PCI_X86_MODULE_NAME, 262 (module_info **)&gPCIx86Module) != B_OK) { 263 ERROR("failed to get pci x86 module\n"); 264 gPCIx86Module = NULL; 265 } 266 267 // find the PCH device (if any) 268 enum pch_info pchInfo = detect_intel_pch(); 269 270 // find devices 271 272 int32 found = 0; 273 274 for (int32 cookie = 0; found < MAX_CARDS;) { 275 pci_info* info = (pci_info*)malloc(sizeof(pci_info)); 276 if (info == NULL) 277 break; 278 279 uint32 type; 280 status = get_next_intel_extreme(&cookie, *info, type); 281 if (status < B_OK) { 282 free(info); 283 break; 284 } 285 286 // create device names & allocate device info structure 287 288 char name[64]; 289 sprintf(name, "graphics/intel_extreme_%02x%02x%02x", 290 info->bus, info->device, 291 info->function); 292 293 gDeviceNames[found] = strdup(name); 294 if (gDeviceNames[found] == NULL) 295 break; 296 297 gDeviceInfo[found] = (intel_info*)malloc(sizeof(intel_info)); 298 if (gDeviceInfo[found] == NULL) { 299 free(gDeviceNames[found]); 300 break; 301 } 302 303 // initialize the structure for later use 304 305 memset(gDeviceInfo[found], 0, sizeof(intel_info)); 306 gDeviceInfo[found]->init_status = B_NO_INIT; 307 gDeviceInfo[found]->id = found; 308 gDeviceInfo[found]->pci = info; 309 gDeviceInfo[found]->registers = info->u.h0.base_registers[0]; 310 gDeviceInfo[found]->device_identifier = kSupportedDevices[type].name; 311 gDeviceInfo[found]->device_type = kSupportedDevices[type].type; 312 gDeviceInfo[found]->pch_info = pchInfo; 313 314 dprintf(DEVICE_NAME ": (%" B_PRId32 ") %s, revision = 0x%x\n", found, 315 kSupportedDevices[type].name, info->revision); 316 317 found++; 318 } 319 320 gDeviceNames[found] = NULL; 321 322 if (found == 0) { 323 mutex_destroy(&gLock); 324 put_module(B_AGP_GART_MODULE_NAME); 325 put_module(B_PCI_MODULE_NAME); 326 if (gPCIx86Module != NULL) { 327 gPCIx86Module = NULL; 328 put_module(B_PCI_X86_MODULE_NAME); 329 } 330 return ENODEV; 331 } 332 333 return B_OK; 334 } 335 336 337 extern "C" void 338 uninit_driver(void) 339 { 340 CALLED(); 341 342 mutex_destroy(&gLock); 343 344 // free device related structures 345 char* name; 346 for (int32 index = 0; (name = gDeviceNames[index]) != NULL; index++) { 347 free(gDeviceInfo[index]); 348 free(name); 349 } 350 351 put_module(B_AGP_GART_MODULE_NAME); 352 put_module(B_PCI_MODULE_NAME); 353 if (gPCIx86Module != NULL) { 354 gPCIx86Module = NULL; 355 put_module(B_PCI_X86_MODULE_NAME); 356 } 357 } 358 359 360 extern "C" device_hooks* 361 find_device(const char* name) 362 { 363 CALLED(); 364 365 int index; 366 for (index = 0; gDeviceNames[index] != NULL; index++) { 367 if (!strcmp(name, gDeviceNames[index])) 368 return &gDeviceHooks; 369 } 370 371 return NULL; 372 } 373