1 /* 2 * Copyright 2012-2014, Artem Falcon <lomka@gero.in> 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <string.h> 8 9 #include <KernelExport.h> 10 11 #include "intel_extreme.h" 12 13 14 #define TRACE_BIOS 15 #ifdef TRACE_BIOS 16 # define TRACE(x) dprintf x 17 #else 18 # define TRACE(x) ; 19 #endif 20 21 22 static const off_t kVbtPointer = 0x1A; 23 24 25 struct vbt_header { 26 uint8 signature[20]; 27 uint16 version; 28 uint16 header_size; 29 uint16 vbt_size; 30 uint8 vbt_checksum; 31 uint8 reserved0; 32 uint32 bdb_offset; 33 uint32 aim_offset[4]; 34 } __attribute__((packed)); 35 36 37 struct bdb_header { 38 uint8 signature[16]; 39 uint16 version; 40 uint16 header_size; 41 uint16 bdb_size; 42 } __attribute__((packed)); 43 44 45 // FIXME the struct definition for the bdb_header is not complete, so we rely 46 // on direct access with hardcoded offsets to get the timings out of it. 47 #define _PIXEL_CLOCK(x) (x[0] + (x[1] << 8)) * 10000 48 #define _H_ACTIVE(x) (x[2] + ((x[4] & 0xF0) << 4)) 49 #define _H_BLANK(x) (x[3] + ((x[4] & 0x0F) << 8)) 50 #define _H_SYNC_OFF(x) (x[8] + ((x[11] & 0xC0) << 2)) 51 #define _H_SYNC_WIDTH(x) (x[9] + ((x[11] & 0x30) << 4)) 52 #define _V_ACTIVE(x) (x[5] + ((x[7] & 0xF0) << 4)) 53 #define _V_BLANK(x) (x[6] + ((x[7] & 0x0F) << 8)) 54 #define _V_SYNC_OFF(x) ((x[10] >> 4) + ((x[11] & 0x0C) << 2)) 55 #define _V_SYNC_WIDTH(x) ((x[10] & 0x0F) + ((x[11] & 0x03) << 4)) 56 57 58 struct lvds_bdb1 { 59 uint8 id; 60 uint16 size; 61 uint8 panel_type; 62 uint8 reserved0; 63 uint16 caps; 64 } __attribute__((packed)); 65 66 67 struct lvds_bdb2_entry { 68 uint16 lfp_info_offset; 69 uint8 lfp_info_size; 70 uint16 lfp_edid_dtd_offset; 71 uint8 lfp_edid_dtd_size; 72 uint16 lfp_edid_pid_offset; 73 uint8 lfp_edid_pid_size; 74 } __attribute__((packed)); 75 76 77 struct lvds_bdb2 { 78 uint8 id; 79 uint16 size; 80 uint8 table_size; /* unapproved */ 81 struct lvds_bdb2_entry panels[16]; 82 } __attribute__((packed)); 83 84 85 struct lvds_bdb2_lfp_info { 86 uint16 x_res; 87 uint16 y_res; 88 uint32 lvds_reg; 89 uint32 lvds_reg_val; 90 uint32 pp_on_reg; 91 uint32 pp_on_reg_val; 92 uint32 pp_off_reg; 93 uint32 pp_off_reg_val; 94 uint32 pp_cycle_reg; 95 uint32 pp_cycle_reg_val; 96 uint32 pfit_reg; 97 uint32 pfit_reg_val; 98 uint16 terminator; 99 } __attribute__((packed)); 100 101 102 static struct vbios { 103 area_id area; 104 uint8* memory; 105 display_mode* shared_info; 106 struct { 107 uint16 hsync_start; 108 uint16 hsync_end; 109 uint16 hsync_total; 110 uint16 vsync_start; 111 uint16 vsync_end; 112 uint16 vsync_total; 113 } timings_common; 114 115 uint16_t ReadWord(off_t address) 116 { 117 return memory[address] | memory[address + 1] << 8; 118 } 119 } vbios; 120 121 122 /*! This is reimplementation, Haiku uses BIOS call and gets most current panel 123 info, we're, otherwise, digging in VBIOS memory and parsing VBT tables to 124 get native panel timings. This will allow to get non-updated, 125 PROM-programmed timings info when compensation mode is off on your machine. 126 */ 127 static bool 128 get_bios(void) 129 { 130 static const uint64_t kVBIOSAddress = 0xc0000; 131 static const int kVBIOSSize = 64 * 1024; 132 // FIXME: is this the same for all cards? 133 134 /* !!!DANGER!!!: mapping of BIOS using legacy location for now, 135 hence, if panel mode will be set using info from VBT, it will 136 be taken from primary card's VBIOS */ 137 vbios.area = map_physical_memory("VBIOS mapping", kVBIOSAddress, 138 kVBIOSSize, B_ANY_KERNEL_ADDRESS, B_KERNEL_READ_AREA, (void**)&vbios.memory); 139 140 if (vbios.area < 0) 141 return false; 142 143 TRACE((DEVICE_NAME ": mapping VBIOS: 0x%" B_PRIx64 " -> %p\n", 144 kVBIOSAddress, vbios.memory)); 145 146 int vbtOffset = vbios.ReadWord(kVbtPointer); 147 if (vbtOffset >= kVBIOSSize) { 148 TRACE((DEVICE_NAME": bad VBT offset : 0x%x\n", vbtOffset)); 149 delete_area(vbios.area); 150 return false; 151 } 152 153 struct vbt_header* vbt = (struct vbt_header*)(vbios.memory + vbtOffset); 154 if (memcmp(vbt->signature, "$VBT", 4) != 0) { 155 TRACE((DEVICE_NAME": bad VBT signature: %20s\n", vbt->signature)); 156 delete_area(vbios.area); 157 return false; 158 } 159 160 return true; 161 } 162 163 164 static bool 165 feed_shared_info(uint8* data) 166 { 167 bool bogus = false; 168 169 /* handle bogus h/vtotal values, if got such */ 170 if (vbios.timings_common.hsync_end > vbios.timings_common.hsync_total) { 171 vbios.timings_common.hsync_total = vbios.timings_common.hsync_end + 1; 172 bogus = true; 173 TRACE((DEVICE_NAME": got bogus htotal. Fixing\n")); 174 } 175 if (vbios.timings_common.vsync_end > vbios.timings_common.vsync_total) { 176 vbios.timings_common.vsync_total = vbios.timings_common.vsync_end + 1; 177 bogus = true; 178 TRACE((DEVICE_NAME": got bogus vtotal. Fixing\n")); 179 } 180 181 if (bogus) { 182 TRACE((DEVICE_NAME": adjusted LFP modeline: x%d Hz,\t" 183 "%d %d %d %d %d %d %d %d\n", 184 _PIXEL_CLOCK(data) / ((_H_ACTIVE(data) + _H_BLANK(data)) 185 * (_V_ACTIVE(data) + _V_BLANK(data))), 186 _H_ACTIVE(data), vbios.timings_common.hsync_start, 187 vbios.timings_common.hsync_end, vbios.timings_common.hsync_total, 188 _V_ACTIVE(data), vbios.timings_common.vsync_start, 189 vbios.timings_common.vsync_end, vbios.timings_common.vsync_total)); 190 } 191 192 /* TODO: add retrieved info to edid info struct, not fixed mode struct */ 193 194 /* struct display_timing is not packed, so we have to set elements 195 individually */ 196 vbios.shared_info->timing.pixel_clock = _PIXEL_CLOCK(data) / 1000; 197 vbios.shared_info->timing.h_display = vbios.shared_info->virtual_width 198 = _H_ACTIVE(data); 199 vbios.shared_info->timing.h_sync_start = vbios.timings_common.hsync_start; 200 vbios.shared_info->timing.h_sync_end = vbios.timings_common.hsync_end; 201 vbios.shared_info->timing.h_total = vbios.timings_common.hsync_total; 202 vbios.shared_info->timing.v_display = vbios.shared_info->virtual_height 203 = _V_ACTIVE(data); 204 vbios.shared_info->timing.v_sync_start = vbios.timings_common.vsync_start; 205 vbios.shared_info->timing.v_sync_end = vbios.timings_common.vsync_end; 206 vbios.shared_info->timing.v_total = vbios.timings_common.vsync_total; 207 208 delete_area(vbios.area); 209 return true; 210 } 211 212 213 bool 214 get_lvds_mode_from_bios(display_mode* sharedInfo) 215 { 216 if (!get_bios()) 217 return false; 218 219 int vbtOffset = vbios.ReadWord(kVbtPointer); 220 struct vbt_header* vbt = (struct vbt_header*)(vbios.memory + vbtOffset); 221 int bdbOffset = vbtOffset + vbt->bdb_offset; 222 223 struct bdb_header* bdb = (struct bdb_header*)(vbios.memory + bdbOffset); 224 if (memcmp(bdb->signature, "BIOS_DATA_BLOCK ", 16) != 0) { 225 TRACE((DEVICE_NAME": bad BDB signature\n")); 226 delete_area(vbios.area); 227 } 228 229 TRACE((DEVICE_NAME": parsing BDB blocks\n")); 230 int blockSize; 231 int panelType = -1; 232 233 for (int bdbBlockOffset = bdb->header_size; bdbBlockOffset < bdb->bdb_size; 234 bdbBlockOffset += blockSize) { 235 int start = bdbOffset + bdbBlockOffset; 236 237 int id = vbios.memory[start]; 238 blockSize = vbios.ReadWord(start + 1) + 3; 239 // TRACE((DEVICE_NAME": found BDB block type %d\n", id)); 240 switch (id) { 241 case 40: // FIXME magic numbers 242 { 243 struct lvds_bdb1 *lvds1; 244 lvds1 = (struct lvds_bdb1 *)(vbios.memory + start); 245 panelType = lvds1->panel_type; 246 break; 247 } 248 case 41: 249 { 250 if (panelType == -1) 251 break; 252 struct lvds_bdb2 *lvds2; 253 struct lvds_bdb2_lfp_info *lvds2_lfp_info; 254 255 lvds2 = (struct lvds_bdb2 *)(vbios.memory + start); 256 lvds2_lfp_info = (struct lvds_bdb2_lfp_info *) 257 (vbios.memory + bdbOffset 258 + lvds2->panels[panelType].lfp_info_offset); 259 /* found bad one terminator */ 260 if (lvds2_lfp_info->terminator != 0xffff) { 261 delete_area(vbios.area); 262 return false; 263 } 264 uint8_t* timing_data = vbios.memory + bdbOffset 265 + lvds2->panels[panelType].lfp_edid_dtd_offset; 266 TRACE((DEVICE_NAME": found LFP of size %d x %d " 267 "in BIOS VBT tables\n", 268 lvds2_lfp_info->x_res, lvds2_lfp_info->y_res)); 269 270 vbios.timings_common.hsync_start = _H_ACTIVE(timing_data) 271 + _H_SYNC_OFF(timing_data); 272 vbios.timings_common.hsync_end 273 = vbios.timings_common.hsync_start 274 + _H_SYNC_WIDTH(timing_data); 275 vbios.timings_common.hsync_total = _H_ACTIVE(timing_data) 276 + _H_BLANK(timing_data); 277 vbios.timings_common.vsync_start = _V_ACTIVE(timing_data) 278 + _V_SYNC_OFF(timing_data); 279 vbios.timings_common.vsync_end 280 = vbios.timings_common.vsync_start 281 + _V_SYNC_WIDTH(timing_data); 282 vbios.timings_common.vsync_total = _V_ACTIVE(timing_data) 283 + _V_BLANK(timing_data); 284 285 vbios.shared_info = sharedInfo; 286 return feed_shared_info(timing_data); 287 } 288 } 289 } 290 291 delete_area(vbios.area); 292 return true; 293 } 294